Categories
JavaScript Answers

How to replace underscores with spaces with JavaScript?

Spread the love

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.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *