24

I have some text which looks like this -

" tushar is a good boy " 

Using javascript I want to remove all the extra white spaces in a string.

The resultant string should have no multiple white spaces instead have only one. Moreover the starting and the end should not have any white spaces at all. So my final output should look like this -

"tushar is a good boy" 

I am using the following code at the moment-

str.replace(/(\s\s\s*)/g, ' ') 

This obviously fails because it doesn't take care of the white spaces in the beginning and end of the string.

6
  • 2
    Can you use string.trim() as a solution? Combining the two lines of code. Commented Aug 5, 2013 at 19:08
  • 1
    @gbam trim() would only trim the beginning and end of the string. that wouldn't account for the "good boy" Commented Aug 5, 2013 at 19:09
  • @sircapsalot, correct. You would combine the two solutions. Trimming the middle ones using regex and the outer ones using trim. I'll edit my comment to clarify. Commented Aug 5, 2013 at 19:10
  • 1
    Do you have newlines that need to be preserved? Or is the only whitespace tabs/spaces? Commented Aug 5, 2013 at 19:12
  • 1
    @JosephMyers: I forgot to mention that, I want to remove new lines and tabs also. Commented Aug 5, 2013 at 19:18

7 Answers 7

37

This can be done in a single String#replace call:

var repl = str.replace(/^\s+|\s+$|\s+(?=\s)/g, ""); // gives: "tushar is a good boy" 
Sign up to request clarification or add additional context in comments.

9 Comments

This is really awesome!
what does (?=\s) do?
(?=\s) makes sure that there is at least one following space NOT included in the match. This way only the extra spaces are removed and not the final space. However, the problem is that whatever the final whitespace character was (tab, newline, etc.,) it will remain and not be replaced with an actual space.
@MathewFoscarini: As Joseph commented, it is actually a positive lookahead that ensures that \s+ is always followed by 1 space. Read more about it here: www.regular-expressions.info/lookaround.html
@anubhava I did not say that the final whitespace will remain. I'm saying that whitespace that is separating words will not be changed to actual spaces if necessary. E.g., "hello[space][space][tab]world" will be changed to "hello[tab]world"
|
8

This works nicely:

function normalizeWS(s) { s = s.match(/\S+/g); return s ? s.join(' ') : ''; } 
  • trims leading whitespace
  • trims trailing whitespace
  • normalizes tabs, newlines, and multiple spaces to a single regular space

3 Comments

Would be be possible to keep line breaks? Ideally, it would keep single line breaks as is (and remove other whitespace around it) and collapse multiple line breaks with other optional whitespace inbetween into two line breaks.
@CoDEmanX Yes, that's possible as well. The efficient way to do what you are asking is to split the entire string into the contents of lines (using \n+ or (?:\r\n)+ as the line separators) and then apply normalizeWS to each line, and then rejoin the lines with a single \n. (Or a \r\n if you wish.)
Good idea, but it requires an additional filtering of the normalized lines to throw away subsequent empty strings to collapse 2+ line breaks into two. I found a way, maybe not the most efficient, but it works as desired: str.match(/[^ \t]+/g).join(' ').replace(/(?:\n[ \t]*){2,}/, '\n\n')
5

Since everyone is complaining about .trim(), you can use the following:

str.replace(/\s+/g,' ' ).replace(/^\s/,'').replace(/\s$/,'');

JSFiddle

1 Comment

+1 Not sure why anyone downvoted this answer. Looks to be the most universally compatible and efficient single-statement answer out of the bunch to me. (Also see: Faster JavaScript Trim for an excellent discussion of the best way to trim a string using JavaScript)
4

Try this:

str.replace(/\s+/g, ' ').trim() 

If you don't have trim add this.

Trim string in JavaScript?

8 Comments

This doesn't do what the OP wants.
OP also wants beginning and end whitespace trimmed.
There is no trim method for strings unless you're using JS 1.8.1
Are you sure the OP also wants to remove newlines? Also, better use {2,} as a quantifier; after all, replacing single spaces with themselves doesn't make much sense.
|
1

This regex may be useful to remove the whitespaces

/^\s+|\s+$/g 

Comments

-1

Try:

str.replace(/^\s+|\s+$/, '') .replace(/\s+/, ' '); 

2 Comments

I don't want to you multiple reg-ex statements. That doesn't seem elegant :)
Multiple reg-ex statements are often more efficient. And I've seen problems where huge, slow single regular expressions could be combined into a few simple ones, but only if the programmer was willing to eschew "elegance" and use multiple statements. Elegant is subjective.
-1

try

var str = " tushar is a good boy "; str = str.replace(/^\s+|\s+$/g,'').replace(/(\s\s\s*)/g, ' '); 

first replace is delete leading and trailing spaces of a string.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.