Regex to add a space after each comma in Javascript

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

    Simplest Solution

    "1,2,3,4".replace(/,/g, ', ')
    //-> '1, 2, 3, 4'
    

    Another Solution

    "1,2,3,4".split(',').join(', ')
    //-> '1, 2, 3, 4'
    
    0 讨论(0)
  • 2021-02-05 11:43

    Use String.replace with a regexp.

    > var input = '1,2,3,4,5',
         output = input.replace(/(\d+,)/g, '$1 ');
    > output
      "1, 2, 3, 4, 5"
    
    0 讨论(0)
提交回复
热议问题