Remove first occurrence of comma in a string

后端 未结 5 1452
甜味超标
甜味超标 2020-12-18 20:51

I am looking for a way to remove the first occurrence of a comma in a string, for example

\"some text1, some tex2, some text3\"

should retu

5条回答
  •  感动是毒
    2020-12-18 21:54

    String.prototype.replace replaces only the first occurence of the match:

    "some text1, some tex2, some text3".replace(',', '')
    // => "some text1 some tex2, some text3"
    

    Global replacement occurs only when you specify the regular expression with g flag.


    var str = ",.,.";
    if (str.match(/,/g).length > 1) // if there's more than one comma
        str = str.replace(',', '');
    

提交回复
热议问题