Replace multiple whitespaces with single whitespace in JavaScript string
I have strings with extra whitespaces, each time there's more than only one whitespace I'd like it be only one. Anyone? I tried searching google, but nothing worked for me. Thanks bjornd Something like this: var s = " a b c "; console.log( s.replace(/\s+/g, ' ') ) You can augment String to implement these behaviors as methods, as in: String.prototype.killWhiteSpace = function() { return this.replace(/\s/g, ''); }; String.prototype.reduceWhiteSpace = function() { return this.replace(/\s+/g, ' '); }; This now enables you to use the following elegant forms to produce the strings you want: "Get