8

How can I remove extra white space (i.e. more than one white space character in a row) from text in JavaScript?

E.g

match    the start using.

How can I remove all but one of the spaces between "match" and "the"?

Paul D. Waite
  • 96,640
  • 56
  • 199
  • 270
ankur
  • 3,951
  • 4
  • 16
  • 7
  • use regex. http://stackoverflow.com/questions/7151159/javascript-regular-expression-remove-spaces – Caleb Oct 03 '13 at 09:31
  • 1
    exact duplicate of [Removing whitespace from string in JavaScript](http://stackoverflow.com/questions/6163169/removing-whitespace-from-string-in-javascript) – Bergi Oct 03 '13 at 10:25

9 Answers9

29

Use regex. Example code below:

var string = 'match    the start using. Remove the extra space between match and the';
string = string.replace(/\s{2,}/g, ' ');

For better performance, use below regex:

string = string.replace(/ +/g, ' ');

Profiling with firebug resulted in following:

str.replace(/ +/g, ' ')        ->  790ms
str.replace(/ +/g, ' ')       ->  380ms
str.replace(/ {2,}/g, ' ')     ->  470ms
str.replace(/\s\s+/g, ' ')     ->  390ms
str.replace(/ +(?= )/g, ' ')    -> 3250ms
Rajesh
  • 3,743
  • 1
  • 24
  • 31
4

See string.replace on MDN

You can do something like this:

var string = "Multiple  spaces between words";
string = string.replace(/\s+/,' ', g);
omgmog
  • 608
  • 3
  • 7
1

Just do,

var str = "match    the start using. Remove the extra space between match and the";
str = str.replace( /\s\s+/g, ' ' );
Ajith S
  • 2,907
  • 1
  • 18
  • 30
1
  function RemoveExtraSpace(value)
  {
    return value.replace(/\s+/g,' ');
  }
Adarsh Kumar
  • 1,134
  • 7
  • 21
1
myString = Regex.Replace(myString, @"\s+", " "); 

or even:

RegexOptions options = RegexOptions.None;
Regex regex = new Regex(@"[ ]{2,}", options);     
tempo = regex.Replace(tempo, @" ");
Waqas Idrees
  • 1,443
  • 2
  • 17
  • 36
1

Using regular expression.

var string = "match    the start using. Remove the extra space between match and the";
string = string.replace(/\s+/g, " ");

Here is jsfiddle for this

Ashok
  • 1,251
  • 11
  • 19
0

Sure, using a regex:

var str = "match    the start using. Remove the extra space between match and the";
str = str.replace(/\s/g, ' ')
Jamie Hutber
  • 26,790
  • 46
  • 179
  • 291
0

This can be done also with javascript logic.
here is a reusable function I wrote for that task.
LIVE DEMO

<!DOCTYPE html>
<html>
  <head>
  </head>
  <body>
    <div>result: 
      <span id="spn">
      </span>
    </div>
    <input type="button" value="click me" onClick="ClearWhiteSpace('match    the start using.  JAVASCRIPT    CAN    BE   VERY  FUN')"/>
    <script>
      function ClearWhiteSpace(text) {
        var result = "";
        var newrow = false;
        for (var i = 0; i < text.length; i++) {
          if (text[i] === "\n") {
            result += text[i];
            // add the new line
            newrow = true;
          }
          else if (newrow == true && text[i] == " ") {
            // do nothing
          }
          else if (text[i - 1] == " " && text[i] == " " && newrow == false) {
            // do nothing
          }
          else {
            newrow = false;
            if (text[i + 1] === "\n" && text[i] == " ") {
              // do nothing it is a space before a new line
            }
            else {
              result += text[i];
            }
          }
        }
        alert(result);
        document.getElementById("spn").innerHTML = result;
        return result;
      }
    </script>
  </body>
</html>
Jonathan Applebaum
  • 5,738
  • 4
  • 33
  • 52
-1

Try this regex

var st = "hello world".replace(/\s/g,'');

or as a function

    function removeSpace(str){
      return str.replace(/\s/g,'');
    }

Here is a working demo

iJade
  • 23,144
  • 56
  • 154
  • 243