116

How can I automatically replace all instances of multiple spaces, with a single space, in Javascript?

I've tried chaining some s.replace but this doesn't seem optimal.

I'm using jQuery as well, in case it's a builtin functionality.

MultiplyByZer0
  • 6,302
  • 3
  • 32
  • 48
Alex
  • 75,813
  • 86
  • 255
  • 348

4 Answers4

247

You could use a regular expression replace:

str = str.replace(/ +(?= )/g,'');

Credit: The above regex was taken from Regex to replace multiple spaces with a single space

Community
  • 1
  • 1
Josiah
  • 4,754
  • 1
  • 20
  • 19
58

There are a lot of options for regular expressions you could use to accomplish this. One example that will perform well is:

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

See this question for a full discussion on this exact problem: Regex to replace multiple spaces with a single space

Community
  • 1
  • 1
Greg Shackles
  • 10,009
  • 2
  • 29
  • 35
44

you all forget about quantifier n{X,} http://www.w3schools.com/jsref/jsref_regexp_nxcomma.asp

here best solution

str = str.replace(/\s{2,}/g, ' ');
redexp
  • 4,765
  • 7
  • 25
  • 37
  • You could also cite [Regular Expression](http://www.regular-expressions.info/) website for all sorts of regex flavors and full reference. – Nick.T Dec 27 '12 at 10:45
  • 3
    What's the improvement in using bracket quantifier over a simple +? – some-non-descript-user Feb 22 '17 at 09:37
  • @some-non-descript-user `/\s+/g` also replaces single spaces (there is no need for this) but `/\s\s+/g` or `/\s{2,}/g` replace only multiple spaces. – Aalex Gabi May 14 '17 at 09:38
  • @Aalex Gabi: sure, you're right, but, aesthetics aside, I doubt this will be noticeable in the vast majority of applications. A slight performance gain in veery large files, maybe? In all other cases, the plus is more convenient than the brackets, I'd say. – some-non-descript-user May 16 '17 at 07:16
  • @some-non-descript-user I agree that the + is more convenient than the brackets. As for performance the single space is one third slower than the double space: https://jsperf.com/removing-multiple-spaces – Aalex Gabi May 16 '17 at 08:46
  • @Aalex Gabi: Thanks for sticking numbers to the performance gain. That's definitively a heads-up for larger tasks. But still, forgive me for being stubborn, you won't notice in the majority of applications. ;) – some-non-descript-user May 16 '17 at 10:28
  • This is perfect, thank you!! – Viper Feb 13 '18 at 19:01
5

You can also replace without a regular expression.

while(str.indexOf('  ')!=-1)str.replace('  ',' ');
kennebec
  • 102,654
  • 32
  • 106
  • 127
  • 1
    Works too... The question is : which is more efficient? This version or the regex version. I have a preference for regex as we never practice enough. – Nick.T Dec 27 '12 at 10:44
  • I don't know- if you have no matches, this method is more efficient. If you have a lot of matches, somebody is doing it wrong. – kennebec Dec 27 '12 at 21:38
  • Here's a [fiddle](http://jsfiddle.net/NickT/ScJKw/1/) that, I hope, emphasize the difference. It clearly outputs that regular expressions are, in all cases, at least as fast as loop versions. Not saying that loop version is bad, just pointing out that one must always consider maintainability and evolution of product. – Nick.T Dec 28 '12 at 07:59