var string = " m a u i"; var nospace = string.replace(/ /g, ''); // "maui"
The same example should be used to replace a substring within another string, for example, if we want to replace all the “a” letters with another:
var string = "I ate pizza tonight."; var newstring = string.replace(/a/g, 'x'); //I xte pizzx tonight.'.