I want to remove all unnecessary commas from the start/end of the string.
eg; google, yahoo,, ,
should become google, yahoo
.
If possible ,google,, , yahoo,, ,
should become google,yahoo
.
I've tried the below code as a starting point, but it seems to be not working as desired.
trimCommas = function(s) {
s = s.replace(/,*$/, "");
s = s.replace(/^\,*/, "");
return s;
}
My take:
This one will work with
opera, internet explorer, whatever
Actually tested this last one, and it works!
match() is much better tool for this than replace()
You should be able to use only one replace call:
Test:
Breakdown:
(?=...)
is a look ahead will will not move the position of the match but only verify that a the characters are after the match. In our case we look for , or EOL