Regex to add a space after each comma in Javascript

前端 未结 8 1459
我寻月下人不归
我寻月下人不归 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:19

    I find important to note that if the comma is already followed by a space you don't want to add the space:

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

    This regex checks the following character and only replaces if its no a space character.

提交回复
热议问题