Regex to add a space after each comma in Javascript

前端 未结 8 1554
我寻月下人不归
我寻月下人不归 2021-02-05 10:39

I have a string that is made up of a list of numbers, seperated by commas. How would I add a space after each comma using Regex?

8条回答
  •  轮回少年
    2021-02-05 11:32

    As I came here and did not find a good generic solution, here is how I did it:

    "1,2, 3,4,5".replace(/,([^\s])/g, ", $1");
    

    This replaces comma followed by anything but a space, line feed, tab... by a comma followed by a space.

    So the regular expression is:

    ,([^\s])
    

    and replaced by

    , $1
    

提交回复
热议问题