How to remove all commas from a string and add white space?

Viewed 23881

Given these two strings:

var a = "November 5, 1916";
var b = "October 5–10, 1592";

I am doing:

b.replace(' ', '').split(' ');
a.replace(' ', '').split(' ');

But I still get a comma and no white space. I need to be able to remove ALL commas from those strings and keep the white space in order to have:

var resultA = "November 5 1916";
var resultB = "October 5–10 1592";

update

I do need the split() afterwards as I need each string in an array.

7 Answers

To replace all occurrences of a string with another string, using the following function:

str.replaceAll(',', '');
Related