user2952265
user2952265

Reputation: 1630

Shorten a regular expression

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

Answers (1)

Wiktor Stribiżew
Wiktor Stribiżew

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

Related Questions