Remove first occurrence of comma in a string

后端 未结 5 1451
甜味超标
甜味超标 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条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-18 21:38

    This will do it:

    if (str.match(/,.*,/)) { // Check if there are 2 commas
        str = str.replace(',', ''); // Remove the first one
    }
    

    When you use the replace method with a string rather than an RE, it just replaces the first match.

提交回复
热议问题