remove unwanted commas in JavaScript

后端 未结 9 1596
青春惊慌失措
青春惊慌失措 2020-12-14 13:14

I want to remove all unnecessary commas from the start/end of the string.

eg; google, yahoo,, , should become google, yahoo.

If pos

相关标签:
9条回答
  • 2020-12-14 13:40

    Not quite as sophisticated, but simple with:

    ',google,, , yahoo,, ,'.replace(/\s/g, '').replace(/,+/g, ',');
    
    0 讨论(0)
  • 2020-12-14 13:43

    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.

    0 讨论(0)
  • 2020-12-14 13:43

    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;
    }
    
    0 讨论(0)
  • 2020-12-14 13:44

    My take:

    var cleanStr = str.replace(/^[\s,]+/,"")
                      .replace(/[\s,]+$/,"")
                      .replace(/\s*,+\s*(,+\s*)*/g,",")
    

    This one will work with opera, internet explorer, whatever

    Actually tested this last one, and it works!

    0 讨论(0)
  • 2020-12-14 13:44

    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);
    
    0 讨论(0)
  • 2020-12-14 13:45

    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).

    0 讨论(0)
提交回复
热议问题