Reputation: 1630
I need a regular expression, that filter numbers from a string without any dot in it. How can I shorten my expression?
myString.replace( /[^\d.]/g, '' ).replace(/\./g, '')
Upvotes: 2
Views: 214
Reputation: 626689
With .replace( /[^\d.]/g, '' )
, you remove any character that is not a digit or a literal dot.
With .replace(/\./g, '')
, you remove all dots.
To combine, just use
myString = myString.replace( /\D/g, '' )
The /\D/g
will match all characters that are not digits.
Upvotes: 2