I searched a lot and can't find the solution for this RegExp (I have to say I'm not very experienced in Reg. Expressions).
I would like to test a number between 1 and 36, excluding 0 and 37 and above.
What I've got so far and almost works (it doesn't accept 17, 18, 19, 27, 28, 29)...
^[1-9]{1}$|^[1-3]{1}[0-6]{1}$|^36$;
Can someone help me please?
Try this:
(All 1 digit numbers between 1 and 9, all 1x and 2x numbers, and 3x numbers from 30 to 36).
Try this:
DEMO
^[0-9]|[0-2][0-9]|3[0-6]$
Here's a breakdown of it:
[0-9]
= any digit from 0-9|
= OR[0-2][0-9]
= '1' or '2', followed by any digit from 0-9|
= OR3[0-6]
= '3', followed by any digit from 0-6.As @mu is too short said, using an integer comparison would be a lot easier, and more efficient. Here's an example function:
Try
^[1-9]$|^[1-2]\d$|^3[0-6]$
I'm not sure why all of the answers to this repeat the mistake of adding boundaries (
^
and$
) before and after each condition. But you only need to do:I also created a JavaScript/Node.js library, to-regex-range, to simplify creating ranges like this.
You know about
\d
, right?Try this in console: