Sometimes, we want to replace all whitespace characters with JavaScript.
In this article, we’ll look at how to replace all whitespace characters with JavaScript.
How to replace all whitespace characters with JavaScript?
To replace all whitespace characters with JavaScript, we can use the string replace
method with a regex.
For instance, we write
str = str.replace(/\s/g, "X");
to call str.replace
with /\s/g
to match all whitespace characters.
\s
matches all whitespace characters.
The g
flag make replace
get all matches.
And we replace them all with 'X'
.
Conclusion
To replace all whitespace characters with JavaScript, we can use the string replace
method with a regex.