Sometimes, we want to replace underscores with spaces with JavaScript.
In this article, we’ll look at how to replace underscores with spaces with JavaScript.
How to replace underscores with spaces with JavaScript?
To replace underscores with spaces with JavaScript, we use the string replace
method.
For instance, we write
const s = str.replace(/_/g, " ");
to call str.replace
with /_/g
to match all underscores in str
.
And we replace them all with ' '
as specified by the 2nd argument.
Then a new string with the replacements done is returned.
Conclusion
To replace underscores with spaces with JavaScript, we use the string replace
method.