How to remove the leading and trailing comma from a string in Node.js

2 Answers

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

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

console.log(str);

  
  
  
  
/*
run:
  
javascript,typescript,node.js,python
  
*/

 



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

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

console.log(str);

  
  
  
  
/*
run:
  
javascript,typescript,node.js,python
  
*/

 



answered Jun 9, 2022 by avibootz

Related questions

...