Regex replace all commas with value

前端 未结 4 1710
长发绾君心
长发绾君心 2021-02-13 12:36

I have a string that looks like this: \"Doe, John, A\" (lastname, firstname, middle initial).

I\'m trying to write a regular expression that converts the string into \"D

4条回答
  •  迷失自我
    2021-02-13 13:16

    String.replace only replaces the first occurence. To replace them all, add the "g" flag for "global". You can also use character groups and the +operator (one or more) to match chains of characters:

    aString.replace("[,\s]+", "*", "g");
    

    This will replace all chains of commas and whitespaces with a *.

提交回复
热议问题