How to remove the leading and trailing comma from a string in TypeScript

2 Answers

0 votes
let str = ',javascript,typescript,node.js,c++,';

str = str.replace(/(^,)|(,$)/g, '');

console.log(str);

  
  
  
  
/*
run:
  
"javascript,typescript,node.js,c++"
  
*/

 



answered Jun 9, 2022 by avibootz
0 votes
let str = ',javascript,typescript,node.js,c++,';

if (str.startsWith(',') && str.endsWith(',')) {
    str = str.slice(1, -1);
}

console.log(str);

  
  
  
  
/*
run:
  
"javascript,typescript,node.js,c++"
  
*/

 



answered Jun 9, 2022 by avibootz
...