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.

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 *