Integer & Decimal Number Regular Expressions

Integer and decimal number regex
Integer and decimal number regex
/(?:\d*\.)?\d+/g 

OR

/(?:\d*\.?\d+)/g

In the above expression we are having various different symbols and I am sure that you all are getting confused with this scary regular expression. But no need to get confused here because it’s the normal expression which matches integers and decimal numbers from given text.

Expression is having set of “/” in the beginning and at the end of the expression which show start and end of the regular expression. Where as ‘g’ is for global search which retains the index of last match and allows iterative searches.

() is a capturing group which groups multiple tokens together and creates a capture group for sub-string extraction.

(?:) denotes the non-capturing group in which the parser uses it to match the group but will not capture it and ignores it later, in the result.

* will match 0 or more times preceding tokens. In the above case (\d*) it will match any digit whether it’s 1, 11, 11, 9, 99, 999, or anything.

\. will match a ‘.’ character.

? will match between 0 & 1 of the preceding token.

\d will look for all the digits

+ will match for 1 or more of preceding token. Like ‘\d+’ will look for 1, 12, 123, 1234, or any number. Alternatively you can also use {1,} for allowing digit or number of length 1 or more which means ‘\d+’ or ‘\d{1,}’ will work similar to each other.

Matching strings: 10, 10.25, 10.500, 0.25, .250

You may also like...