Strip all non-numeric characters from string in JavaScript
Use the string's .replace
method with a regex of \D
, which is a shorthand character class that matches all non-digits:
myString = myString.replace(/\D/g,'');
If you need this to leave the dot for float numbers, use this
var s = "-12345.50 €".replace(/[^\d.-]/g, ''); // gives "-12345.50"
Use a regular expression, if your script implementation supports them. Something like:
myString.replace(/[^0-9]/g, '');