javascript 正则表达式 - 使用两种不同的模式验证帐号
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22749891/
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
Regex - Validate an account number with two different patterns
提问by eddy
I need to validate an account number. A valid number could be either a sequence of exactly 11digits or 3 groups of digits separated by hyphens (2 digits -3 digits -6 digits)
我需要验证一个帐号。有效数字可以是正好11位的序列或由连字符分隔的 3 组数字(2 位 -3 位 -6 位)
I tried this :
我试过这个:
/^([0-9]{11})|([0-9]{2}-[0-9]{3}-[0-9]{6})$/
But it only works for the second rule . The first rule doesn't work as it allows numbers of more than 11 digits
但它只适用于第二条规则。第一条规则不起作用,因为它允许超过 11 位的数字
This is how I use the regex in my js function :
这就是我在我的 js 函数中使用正则表达式的方式:
var re = /^([0-9]{11})|([0-9]{2}-[0-9]{3}-[0-9]{6})$/;
if (re.test(txtNumber.value)==true) {
return 1;
}
else {
alert("Invalid Account Number");
return 0;
}
Any advise or guidance would be greatly appreciated
任何建议或指导将不胜感激
VALID NUMBERS:
有效号码:
12345678912 (11 digits)
12345678912(11位)
12-345-678912 (11 digits separated by hyphens)
12-345-678912(以连字符分隔的 11 位数字)
INVALID NUMBERS:
无效号码:
1223 (less than 11 digits)
1223(小于11位)
111111111111 ( more than 11 digits)
111111111111(11位以上)
123-23-678912 (11 digits, but not separated correctly, it should be 2 digits-3 digits-6 digits)
123-23-678912(11位,但没有正确分隔,应该是2位-3位-6位)
回答by raina77ow
As |
regex operator precedence is the lowest, it should be written like this:
由于|
正则表达式运算符的优先级最低,所以应该这样写:
/^(?:[0-9]{11}|[0-9]{2}-[0-9]{3}-[0-9]{6})$/
... so that alternation pattern is bound both to the beginning and the end of the string.
...以便交替模式绑定到字符串的开头和结尾。
As it stands in your code, pattern checks for either sequence of 11 digits at the beginning of the string, or sequence of 'two digits, hyphen, three digits, hyphen, six digits' at its end- but never really bind the rule to both ends. And that's easy to prove:
目前的情况是在你的代码,模式检查的11位或者序列在字符串的开头的“两个数字,连字符,三个数字,连字符,六位数字”,或序列在其结束-但从来没有真正绑定的规则两端。这很容易证明:
var patt = /^([0-9]{11})|([0-9]{2}-[0-9]{3}-[0-9]{6})$/;
patt.test('acdbdfdsfsf22-333-666666'); // true
As a sidenote, as you don't need to capture anything with that grouping expression, I've prepended it with ?:
. Actually, it can be optimized even more:
作为旁注,由于您不需要使用该分组表达式捕获任何内容,因此我在它前面加上了?:
. 实际上,它可以进一步优化:
/^[0-9]{2}(?:[0-9]{9}|-[0-9]{3}-[0-9]{6})$/
... as the less you alternate, the better. But in this case it really won't matter much, I suppose.
......因为你交替的越少越好。但在这种情况下,我想它真的没有多大关系。
In short, the problem can be illustrated with these two patterns:
简而言之,这个问题可以用这两种模式来说明:
/^a|b$/
This reads as 'match either a at the beginning of the string
, or b at its end
.
这读作'匹配要么a at the beginning of the string
,要么b at its end
。
/^(?:a|b)$/
This reads as 'match the beginning of the string
, followed by either a
or b
, followed by the end of the string
'.
这读作“匹配beginning of the string
,然后是a
或b
,然后是the end of the string
”。