Sometimes, we want to remove numbers from a string using JavaScript.
In this article, we’ll look at how to remove numbers from a string using JavaScript.
How to remove numbers from a string using JavaScript?
To remove numbers from a string using JavaScript, we can use the JavaScript string’s replace
method with a regex.
For instance, we write:
const string = 'All23';
const newString = string.replace(/\d+/g, '')
console.log(newString)
to return a string that has all the numbers in string
removed.
We call replace
with a regex that matches all digits and replace them with empty strings.
As a result, newString
is 'All'
.
Conclusion
To remove numbers from a string using JavaScript, we can use the JavaScript string’s replace
method with a regex.