Categories
JavaScript Answers

How to remove hyphens from a string with JavaScript?

Spread the love

Sometimes, we want to remove hyphens from a string with JavaScript.

In this article, we’ll look at how to remove hyphens from a string with JavaScript.

How to remove hyphens from a string with JavaScript?

To remove hyphens from a string with JavaScript, we can use the string replace method.

For instance, we write

const str = "185-51-671";
const newStr = str.replace(/-/g, "");

to call str.replace with /-/g and an empty string to find all hyphens in str and replace them with empty strings.

The new string is returned.

Conclusion

To remove hyphens from a string with JavaScript, we can use the string replace method.

Leave a Reply

Your email address will not be published.

If you like the content of this blog, subscribe to my email list to get exclusive articles not available to anyone else.