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.