29

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?

3
  • 2
    You dont' necessarily need to use regex if you only want a space after each comma. Replace each comma with a comma and a space. Commented Oct 1, 2011 at 15:19
  • 2
    @Chase: Replacing all occurences is usually best done with a regexp though (with the g flag). Commented Oct 1, 2011 at 15:26
  • @pimvdb My mistake. I was under the impression Javascript's replace method replaces all occurrences by default, not just the first match. Commented Oct 1, 2011 at 15:43

8 Answers 8

39

Simplest Solution

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

Another Solution

"1,2,3,4".split(',').join(', ')
//-> '1, 2, 3, 4'
Sign up to request clarification or add additional context in comments.

2 Comments

What if there are multiple spaces after comma? like "1, 2,3,4" or "1, 2,3,4". It works when there is no space after comma but if single or multiple comma already exists, it will add additional space to existing space. I think, we don't want that.
Just update the RegExp to you needs: .replace(/ *, */g, ', ')
28

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.

Comments

12

Another simple generic solution for comma followed by n spaces:

"1,2, 3,   4,5".replace(/,[s]*/g, ", ");
> "1, 2, 3, 4, 5"

Always replace comma and n spaces by comma and one space.

1 Comment

This is wrong. It removes 's' from your string. Try This: "s1,s2,s3,s4,s5".replace(/,[s]*/g, ", ")
7

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"

4 Comments

why such a complicated pattern for this task? Why not just replace ',' with ', '? Perhaps a better pattern would do a lookahead and only replace commas that don't already have a space after them.
There are many ways to skin this cat. Yes, input.replace(/,/g, ', ') would also work. Does it really matter?
Does it matter? I think it does. When teaching someone how to program we should teach them to strive for the simplest solution.
When I have 2 regular expressions to check, I look for the differences. In this case: What if there isn't a digit in front of one of the commas? What if there is a comma at the start of the string, or two commas next to each other? If those cases will never happen, or you don't care what happens in those cases, then there's no need to use the more complicated version
6

Those are all good ways but in cases where the input is made by the user and you get a list like "1,2, 3,4, 5,6,7"

..In which case lets make it idiot proof! So accounting for the already formatted parts of the string, the solution:

"1,2, 3,4, 5,6,7".replace(/, /g, ",").replace(/,/g, ", ");

//result: "1, 2, 3, 4, 5, 6, 7" //Bingo!

Comments

3
var numsStr = "1,2,3,4,5,6";
var regExpWay = numStr.replace(/,/g,", ");
var splitWay = numStr.split(",").join(", ");

1 Comment

The split and join method is the faster of the two in this case. You can run a test on the speed difference here: jsperf.com/replace-vs-split-join/6
3

Don't use a regex for this, use split and join.

It's simpler and faster :)

'1,2,3,4,5,6'.split(',').join(', '); // '1, 2, 3, 4, 5, 6'

Comments

2

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.