Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

numeric/string to only numeric

Tags:

javascript

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

like image 729
Pedro Avatar asked Apr 09 '26 00:04

Pedro


2 Answers

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
like image 101
Andy E Avatar answered Apr 10 '26 14:04

Andy E


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);
like image 40
Guffa Avatar answered Apr 10 '26 14:04

Guffa



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!