remove unwanted commas in JavaScript

2020-02-08 09:21发布

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;
}

9条回答
We Are One
2楼-- · 2020-02-08 09:46

First ping on Google for "Javascript Trim": http://www.somacon.com/p355.php. You seem to have implemented this using commas, and I don't see why it would be a problem (though you escaped in the second one and not in the first).

查看更多
够拽才男人
3楼-- · 2020-02-08 09:51

What you need to do is replace all groups of "space and comma" with a single comma and then remove commas from the start and end:

trimCommas = function(str) {
    str = str.replace(/[,\s]*,[,\s]*/g, ",");
    str = str.replace(/^,/, "");
    str = str.replace(/,$/, "");
    return str;
}

The first one replaces every sequence of white space and commas with a single comma, provided there's at least one comma in there. This handles the edge case left in the comments for "Internet Explorer".

The second and third get rid of the comma at the start and end of string where necessary.

You can also add (to the end):

str = str.replace(/[\s]+/, " ");

to collapse multi-spaces down to one space and

str = str.replace(/,/g, ", ");

if you want them to be formatted nicely (space after each comma).

A more generalized solution would be to pass parameters to indicate behaviour:

  • Passing true for collapse will collapse the spaces within a section (a section being defined as the characters between commas).
  • Passing true for addSpace will use ", " to separate sections rather than just "," on its own.

That code follows. It may not be necessary for your particular case but it might be better for others in terms of code re-use.

trimCommas = function(str,collapse,addspace) {
    str = str.replace(/[,\s]*,[,\s]*/g, ",").replace(/^,/, "").replace(/,$/, "");
    if (collapse) {
        str = str.replace(/[\s]+/, " ");
    }
    if (addspace) {
        str = str.replace(/,/g, ", ");
    }
    return str;
}
查看更多
Bombasti
4楼-- · 2020-02-08 09:58

In your example you also want to trim the commas if there's spaces between them at the start or at the end, use something like this:

str.replace(/^[,\s]+|[,\s]+$/g, '').replace(/,[,\s]*,/g, ',');

Note the use of the 'g' modifier for global replace.

查看更多
干净又极端
5楼-- · 2020-02-08 09:59

Not quite as sophisticated, but simple with:

',google,, , yahoo,, ,'.replace(/\s/g, '').replace(/,+/g, ',');
查看更多
我欲成王,谁敢阻挡
6楼-- · 2020-02-08 10:05

When you want to replace ",," ",,,", ",,,," and ",,,,," below code will be removed by ",".

var abc = new String("46590,26.91667,75.81667,,,45346,27.18333,78.01667,,,45630,12.97194,77.59369,,,47413,19.07283,72.88261,,,45981,13.08784,80.27847,,");
var pqr= abc.replace(/,,/g,',').replace(/,,/g, ',');

alert(pqr);
查看更多
Summer. ? 凉城
7楼-- · 2020-02-08 10:06

You need this:

s = s.replace(/[,\s]{2,}/,""); //Removes double or more commas / spaces
s = s.replace(/^,*/,""); //Removes all commas from the beginning
s = s.replace(/,*$/,""); //Removes all commas from the end

EDIT: Made all the changes - should work now.

查看更多
登录 后发表回答