1

I have the following regular expression which performs very basic validation to allow only integers and should not allow any floating point numbers and negative numbers. I found the below expression as one of the ways to do so:

var reg = new RegExp('^(([0-9]+)|\.[0-9]+)$');

This expression correctly validates the following inputs:

validatePercentage(56.67); --> not an integer
validatePercentage(67); --> integer
validatePercentage(-5667); --> should not be negative

However, I'm unable to understand the usage of '\.' in the expression (that somehow seems to be making a difference to the output). Can somebody please explain how exactly is the regex working to eliminate negative inputs? Thanks in advance.

Awani
  • 394
  • 3
  • 7
  • 19
  • 1
    `\.` matches the character `.` (dot) as is – abhishekkannojia Dec 21 '15 at 09:35
  • 2
    That regular expression is **not** for checking if a number is an integer. It would pass a string `z0` for example, which is definitely not an integer. This question is a perfect demonstration that "I found the below expression" does not work in real life. And that programming is about writing code, not copy-paste-adapt'ing it. – zerkms Dec 21 '15 at 09:35
  • Possible duplicate of [Reference - What does this regex mean?](http://stackoverflow.com/questions/22937618/reference-what-does-this-regex-mean) – Biffen Dec 21 '15 at 09:36
  • Hi zerkms, tried the string that you specified (z0) and it did not pass it. – Awani Dec 21 '15 at 09:48

2 Answers2

9
  1. . in a regex means "any character"
  2. \. in a regex means "a period character"
  3. \. in a JavaScript string literal means ..

This code is an attempt at using 2, but because the regex is being constructed using a string (almost always a mistake) it ends up being a 3 which gets converted into a 1.


The regex appears to be an attempt at "Zero or a positive integer or a positive float that is less than one" but is mangled by the string phase to be "Zero or a positive integer either of which may be preceded by any character at all".

The correct regex would probably be something more like:

var reg = /^[1-9][0-9]+(?:\.0+)?$/

If you really want a test for a positive integer then you'd probably be better off with:

value == parseInt(value, 10) && value > 0

(or > -1 if you want to allow 0 as a value).

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
  • Also I have a good question about regex. May you please take a look at [it](http://stackoverflow.com/questions/34406286/how-to-prevent-of-re-replacing-by-second-regex)? – stack Dec 22 '15 at 01:23
3

To figure a literal dot in a pattern, the dot must be escaped, otherwise the dot may match any characters except newlines.

When you use the object notation with a javascript string to define a pattern, backslashes must be escaped that gives:

var reg = new RegExp('^(([0-9]+)|\\.[0-9]+)$');

or

var reg = /^(([0-9]+)|\.[0-9]+)$/;

You can write too:

var reg = new RegExp(/^(([0-9]+)|\.[0-9]+)$/);
Casimir et Hippolyte
  • 88,009
  • 5
  • 94
  • 125