I have a string that is like below.
,liger, unicorn, snipe,
how can I trim the leading and trailing comma in javascript?
If you want to make sure you don't have any trailing commas or whitespace, you might want to use this regex.
var str = ' , , , foo, bar, '; str = str.replace(/(^[,\s]+)|([,\s]+$)/g, '');
returns
"foo, bar"