Sometimes, we want to use a regex to remove letters and symbols except numbers with JavaScript.
In this article, we’ll look at how to use a regex to remove letters and symbols except numbers with JavaScript.
How to use a regex to remove letters and symbols except numbers with JavaScript?
To use a regex to remove letters and symbols except numbers with JavaScript, we can use the string replace
method.
For instance, we write
const removedText = str.replace(/\D+/g, "");
to call str.replace
with /\D+/g
and an empty string to return a new string with the non-digit characters in str
removed.
Conclusion
To use a regex to remove letters and symbols except numbers with JavaScript, we can use the string replace
method.