Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
47
rated 0 times [  48] [ 1]  / answers: 1 / hits: 43133  / 13 Years ago, fri, october 14, 2011, 12:00:00

Possible Duplicate:

Replace multiple whitespaces with single whitespace in JavaScript string




I have used trim function to remove the white spaces at the beginning and end of the sentences. If there is to much white spaces in between words in a sentence, is there any method to trim?


for example


"abc                  def. fds                            sdff."

More From » regex

 Answers
43

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?

[#89613] Thursday, October 13, 2011, 13 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
madelyn

Total Points: 449
Total Questions: 100
Total Answers: 100

Location: Seychelles
Member since Fri, May 7, 2021
3 Years ago
madelyn questions
Wed, Jul 28, 21, 00:00, 3 Years ago
Wed, Jul 14, 21, 00:00, 3 Years ago
Sat, Nov 7, 20, 00:00, 4 Years ago
Thu, Sep 3, 20, 00:00, 4 Years ago
;