try
"abc def. fds sdff."
.replace(/\s+/g,' ')
or
"abc def. fds sdff."
.split(/\s+/)
.join(' ');
or use this allTrim
String extension
String.prototype.allTrim = String.prototype.allTrim ||
function(){
return this.replace(/\s+/g,' ')
.replace(/^\s+|\s+$/,'');
};
//usage:
alert(' too much whitespace here right? '.allTrim());
//=> "too much whitespace here right?"