Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex to add a space after each comma in Javascript

Tags:

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?

like image 454
Shealan Avatar asked Oct 01 '11 15:10

Shealan


People also ask

How do you put a space after a comma?

Commas separate parts of a sentence into logical elements. Commas have no meaning, but they help us to see the structure and therefore the meaning of the sentence. Put a space after a comma. Do not put a space before a comma.

How do you add a space in Javascript?

Use the padEnd() and padStart() methods to add spaces to the end or beginning of a string, e.g. str. padEnd(6, ' '); . The methods take the maximum length of the new string and the fill string and return the padded string. Copied!


Video Answer


1 Answers

Simplest Solution

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

Another Solution

"1,2,3,4".split(',').join(', ')
//-> '1, 2, 3, 4'
like image 186
kzh Avatar answered Oct 10 '22 04:10

kzh