64

How can I remove all extra space between words in a string literal?

"some value" 

Should become

"some value" 

Also,

" This should become something else too . " 

Becomes

"This should become something else too ." 

Do not worry about moving the .. Just as above is fine. I know I can use $.trim(str) to achieve the trailing/ending space removal. But, I'm not sure how to do the 1 space between words trick.

4
  • regex to the rescue....! Commented Oct 3, 2011 at 13:54
  • have you tried to google "remove all extra spacing between words javascript" ? The third link is what you are looking for lawrence.ecorp.net/inet/samples/regexp-format.php Commented Oct 3, 2011 at 13:57
  • @Massimiliano Peluso - Yes, that's how I found $.trim(). Also, I learn a lot more from SO than I do from a search engine. Commented Oct 3, 2011 at 14:05
  • "The wise man is not the the one who got the knowledge but is the one who knows how to find what he was looking for" :-) Commented Oct 3, 2011 at 14:09

8 Answers 8

119
var string = " This should become something else too . "; string = string.replace(/\s+/g, " "); 

This code replaces a consecutive set of whitespace characters (\s+) by a single white space. Note that a white-space character also includes tab and newlines. Replace \s by a space if you only want to replace spaces.

If you also want to remove the whitespace at the beginning and end, include:

string = string.replace(/^\s+|\s+$/g, ""); 

This line removes all white-space characters at the beginning (^) and end ($). The g at the end of the RegExp means: global, ie match and replace all occurences.

Sign up to request clarification or add additional context in comments.

1 Comment

var newString = string.replace(/^\s+|\s+$/g, "").replace(/\s+/g, " ");
22
var str = " This should become something else too . "; str = str.replace(/ +(?= )/g,''); 

Here's a working fiddle.

6 Comments

A little explanation on the regex used would be nice!
Why such a complicated reg exp when all that is needed is \s+? And why lookaheads are not good.
it means you are replacing one or more space characters / + - that precede a space character (?= ) throughout the whole string /g with nothing ,'' . You can read more about it here
' Hello world '.replace(/\s+/g, " ").trim();
@Shiala, as with all things in programming, there are multiples ways to accomplish a goal (btw, this question was answered in 2011). If you would like to contribute a better answer, do it in an answer, not a comment.
|
12

In case we want to avoid the replace function with regex,

We can achieve same result by

str.split(' ').filter(s => s).join(' ') // var str = " This should become something else too . "; // result is "This should become something else too ." 

First, split the original string with space, then we will have empty string and words in an array. Second, filter to remain only words, then join all words with a whitespace.

3 Comments

please describe more
why this filter work?
@hamidrezaghanbari the paragraph below the code is my description for the code, after split with space, then you will have only empty string and string, filter the empty string then left words, then join back words with single space then become normal sentence
4
var str = " This should become something else too . " $.trim(str).replace(/\s(?=\s)/g,'') 

This uses lookahead to replace multiple spaces with a single space.

Comments

3

jsFiddle Example

" This should become something else too . ".replace(/[\s\t]+/g,' '); 

Comments

2

Another (perhaps easier to understand) regexp replacement that will do the trick:

var input = /* whatever */; input = input.replace(/ +/g, ' '); 

The regexp matches one or more spaces, so the .replace() call replaces every single or repeated space with a single space.

Comments

0
var str = 'some value'; str.replace(/\s\s+/g, ' '); 

1 Comment

why the extra \s? There is no need for that.
0

For my case, I had to combine other answers and add the /m modifier for a multiline string, resulting in:

string.replace(/^[ \s\t]*| +(?= )/gm, '') 

let string = ` I want all the extra spaces removed. ^ `; console.log(string.replace(/^[ \s\t]*| +(?= )/gm, ''))

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.