Sometimes, we want to use regex to replace everything except numbers and a decimal point with JavaScript.
In this article, we’ll look at how to use regex to replace everything except numbers and a decimal point with JavaScript.
How to use regex to replace everything except numbers and a decimal point with JavaScript?
To use regex to replace everything except numbers and a decimal point with JavaScript, we can use the string replace
method.
For instance, we write
const n = number.replace(/(\.\d+)+/, "");
to replace all digits after the decimal point with empty strings.
Conclusion
To use regex to replace everything except numbers and a decimal point with JavaScript, we can use the string replace
method.