RegExp range of number (1 to 36)
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?
You know about \d
, right?
^([1-9]|[12]\d|3[0-6])$
Try this in console:
function test() {
for(var i = 0; i < 100; i++) {
if (/^([1-9]|[12]\d|3[0-6])$/.test(i.toString()) != (i >= 1 && i <=36)) {
document.write(i + "fail");
}
else
document.write(i + "pass");
document.write("<br/>");
}
}
^(?:[1-9]|[1-2][0-9]|3[0-6])$
Here's a breakdown of it:
^
= Start of line
(?:
and )
demark a non-capturing group- a way to specify order of operations without saving the matched contents for later.
[1-9]
= any digit from 1-9
|
= OR
[1-2][0-9]
= '1' or '2', followed by any digit from 0-9
|
= OR
3[0-6]
= '3', followed by any digit from 0-6.
$
= end of line
As @mu is too short said, using an integer comparison would be a lot easier, and more efficient. Here's an example function:
function IsInRange(number)
{
return number > 0 && number < 37;
}
Try this:
^[1-9]$|^[1-2][0-9]$|^3[0-6]$
(All 1 digit numbers between 1 and 9, all 1x and 2x numbers, and 3x numbers from 30 to 36).
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:
^([1-9]|[1-2][0-9]|3[0-6])$
I also created a JavaScript/Node.js library, to-regex-range, to simplify creating ranges like this.
Try this:
/^[1-9]$|^[1-2]\d$|^3[0-6]$/
DEMO
Try ^[1-9]$|^[1-2]\d$|^3[0-6]$
Purely for academic reasons, I'll add a unique and accurate pattern.
/^(3[0-6]?|[12]\d?|[4-9])$/
There are three branches in the pattern:
- The first matches: 3, 30, 31, 32, 33, 34, 35, 36
- The second matches: 1, 2, 10-19, 20-29
- The third matches: 4, 5, 6, 7, 8, 9
If there is an efficiency advantage (not that this task is likely to be a major resource drain -- unless you are doing thousands of these evaluations) in my pattern, it will come down to the fact that there are no redundant checks in the pattern.
It may not make any difference to the regex engine, but I've ordered my branches based on the ones that take the least "effort" to evaluate (instead of the natural sequence of numbers).
Harpo's pattern is as brief as the pattern can be built, but [123]
are first-digit matches that are satisfied by the multiple branches.
@MichaelHoffmann's and @jonschlinkert's patterns are not correct because they fail to distribute the necessary anchors to each branch. This is concisely achieved by wrapping all branches in a capture group, but as @ninjalj, @qwertymk, and @amit_g demonstrated, it is just as accurate to apply the anchors to each branch.
let txt = '<table border=1 cellpadding=4><tr><th>Number</th><th>Harpo</th><th>Michael Hoffmann</th><th>ninjalj</th><th>jonschlinkert</th><th>qwertymk</th><th>amit_g</th><th>mickmackusa</th></tr>',
str,
i;
for (i = 0; i <= 40; ++i) {
str = '' + i;
txt += '<tr><td>' + i;
txt += '</td><td>' + /^([1-9]|[12]\d|3[0-6])$/.test(str);
txt += '</td><td>' + /^[0-9]|[0-2][0-9]|3[0-6]$/.test(str);
txt += '</td><td>' + /^[1-9]$|^[1-2][0-9]$|^3[0-6]$/.test(str);
txt += '</td><td>' + /^[1-9]|[1-2][0-9]|3[0-6]$/.test(str);
txt += '</td><td>' + /^[1-9]$|^[1-2]\d$|^3[0-6]$/.test(str);
txt += '</td><td>' + /^[1-9]$|^[1-2]\d$|^3[0-6]$/.test(str);
txt += '</td><td>' + /^(3[0-6]?|[12]\d?|[4-9])$/.test(str);
txt += '</td></tr>';
}
txt = txt + '</table>';
document.getElementById('test').innerHTML = txt;
<div id="facts">Correct Pattern Designers: Harpo, ninjalj, qwertymk, amit_g, mickmackussa<br>Incorrect Patterns: Michael Hoffmann, jonschlinkert <br></div>
<div id="test"></div>
Check run this in console Try this:
var pat = /^[1-9]$|^[1-2]\d$|^3[0-6]$/;
for(var i = 0; i < 100; i++) {
if (pat.test(i)) {
document.write(i + "pass");
}
else{
document.write(i + "fail");}
document.write("<br/>");
}
(All digit numbers between 1 to 36).
精彩评论