To remove all whitespaces from a string in JavaScript, you can use the String.replace()
method.
The String.replace()
method takes a string/regex that is to be replaced as the first parameter and the string that should be used as a replacement string as a second parameter.
To remove all the whitespaces, pass a regular expression that matches any whitespace characters in the string as a first parameter to the replace()
method and an empty string(''
) as a second parameter.
This will replace all the whitespace characters with an empty string(''
).
For example,
// String with spaces const str = " Hello World! "; // Remove whitespaces from the string const strWithoutSpaces = str.replace(/\s/g,''); // Print the result console.log(strWithoutSpaces);
Output:
"HelloWorld!"
In this example, the regular expression /\s/g
matches any whitespace character (including spaces, tabs, and line breaks) in the string, and the replace()
method replaces all matches with an empty string(''
).
In the RegEx /\s/g
:
//
: Indicates the start and end of the string\s
: matches any whitespace character such as tab, space, line break, etc.g
: theg
flag searches for the match in the entire string(globally)
We used the g
flag to specify that we want to remove all the whitespaces from the string. Without the g
flag only the first white space will be removed from the string.
// String with spaces const str = " Hello World! "; // Only first whitespace is removed(without g flag) const newStr = str.replace(/\s/, ''); console.log(newStr); // Output: 👉 " Hello World! "
If you want to replace all the whitespace characters with some other string or character, you can pass it as a second parameter to the replace()
method.
The following example replaces all the whitespace characters with an underscore(_
):
// String with spaces const str = " Hello World! "; // Replace whitespaces with underscore(_) const strWithUnderscore = str.replace(/\s/g, '_'); // Print the result console.log(strWithUnderscore);
Output:
"__Hello___World!___"
Use the split() and join() Methods to Remove all Whitespaces
You can also use the String.split()
and Array.join()
methods in combination to remove all whitespaces from a string.
The String.split()
method splits the given string into an array of substrings based on the specified delimiter or regular expression.
On the other hand, the Array.join()
method joins the elements of the array into a string with a specified separator.
So, if we want to remove all the whitespaces from a string, we have to first split the string at each occurrence of the space character(' '
) using the split()
method and then join back each substring with an empty character(''
) using the join()
method.
For example:
// String with spaces const str = " Hello World! "; // Split string at space(' ') character and join back const strWithoutSpaces = str.split(' ').join(''); // Print the result console.log(strWithoutSpaces);
Output:
"HelloWorld!"
Please note that the original string isn’t modified in any case.
Remove All Spaces from a String using replaceAll() Method
If you only want to remove all spaces(excluding tabs and line breaks) from a string, you can directly call the replaceAll()
method on it with a string parameter instead of passing a regular expression.
The first parameter will be a string with an empty space(''
) and the second parameter will be an empty string. The replaceAll() will replace each occurrence of the space character with an empty string, therefore, all the spaces will be removed from the string.
// String with spaces const str = " Hello World! "; // Replace spaces(' ') with empty string('') const strWithoutSpaces = str.replaceAll(' ', ''); // Print the result console.log(strWithoutSpaces);
Output:
"HelloWorld!"
Please note that this method will not remove line breaks and tab spaces.
That’s all for the day. Happy Coding.