In Javascript, how transform a "numeric/string" variable to only numeric? Using some function or regular expression.?
Sample: Input: 11.489.301/0001-47 to: 11489301000147
Use replace() to remove any non-numeric characters, and Number() to convert the string type to a number type:
var num = Number("11.489.301/0001-47".replace(/\D/g, ""));
alert(num);
//-> 11489301000147
If you mean to just remove everything that is not digits from a string you can use a regular expression:
s = s.replace(/\D+/g,'');
If you then want to turn the string it into a numeric value, you can parse it:
var n = parseInt(s,10);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With