12

im new with regexp, so can i ask for some assistance

Using string.replace function what code that can replace spaces with comma

Input:The quick brown fox jumps over the lazy dog. Output:The,quick,brown,fox,jumps,over,the,lazy dog.

Thanks

Alan Moore
  • 73,866
  • 12
  • 100
  • 156
Treby
  • 1,328
  • 6
  • 18
  • 26

1 Answers1

29

Like this:

str = str.replace(/ /g, ',');

Here's a better one which will replace all strings of whitespace:

str = str.replace(/\s+/g, ',');
SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964