How to remove duplicate white spaces in a string? [duplicate]

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?"

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)