Javascript RegExp 数字范围(1 到 36)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5811528/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
RegExp range of number (1 to 36)
提问by HymanJoe
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).
我搜索了很多,找不到这个 RegExp 的解决方案(我不得不说我在 Reg. Expressions 方面不是很有经验)。
I would like to test a number between 1 and 36, excluding 0 and 37 and above.
我想测试一个 1 到 36 之间的数字,不包括 0 和 37 及以上。
What I've got so far and almost works (it doesn't accept 17, 18, 19, 27, 28, 29)...
到目前为止我所拥有的几乎可以工作(它不接受 17、18、19、27、28、29)......
^[1-9]{1}$|^[1-3]{1}[0-6]{1}$|^36$;
Can someone help me please?
有人能帮助我吗?
回答by harpo
You know about \d
, right?
你知道\d
,对吧?
^([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/>");
}
}
回答by Michael Hoffmann
^[0-9]|[0-2][0-9]|3[0-6]$
^[0-9]|[0-2][0-9]|3[0-6]$
Here's a breakdown of it:
这是它的细分:
[0-9]
[0-9]
= 0-9 之间的任何数字|
|
= 或[0-2][0-9]
[0-2][0-9]
= '1' 或 '2',后跟 0-9 之间的任何数字|
|
= 或3[0-6]
3[0-6]
= '3',后跟 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:
正如@mu 所说的那样,使用整数比较会更容易、更有效。这是一个示例函数:
function IsInRange(number)
{
return number > 0 && number < 37;
}
回答by ninjalj
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).
(1 到 9 之间的所有 1 位数字,所有 1x 和 2x 数字,以及 30 到 36 之间的 3x 数字)。
回答by jonschlinkert
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.
我还创建了一个 JavaScript/Node.js 库to-regex-range,以简化创建这样的范围。
回答by amit_g
Try ^[1-9]$|^[1-2]\d$|^3[0-6]$
尝试 ^[1-9]$|^[1-2]\d$|^3[0-6]$
回答by mickmackusa
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
- 第一场比赛:3, 30, 31, 32, 33, 34, 35, 36
- 第二场比赛:1, 2, 10-19, 20-29
- 第三场比赛: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.
Harpo 的模式与可以构建的模式一样简短,但[123]
都是多个分支满足的第一位数字匹配。
@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.
@MichaelHoffmann 和 @jonschlinkert 的模式不正确,因为它们未能将必要的锚点分发到每个分支。这是通过将所有分支包装在一个捕获组中来简洁地实现的,但正如 @ninjalj、@qwertymk 和 @amit_g 所展示的那样,将锚点应用于每个分支同样准确。
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>