Javascript 正则表达式 - 9 位数字
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11988445/
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
Javascript Regex - 9-digit number
提问by Dan
I am trying to validate a variable contains a 9-digit number in Javascript. This should be basic but for some reason this is failing:
我正在尝试在 Javascript 中验证包含 9 位数字的变量。这应该是基本的,但由于某种原因,这是失败的:
var test = "123123123";
var pattern = new RegExp("/\d{9}/");
if(test.match(pattern))
{
//This code never executes
}
else
{
//This code is always executing
alert('no match!');
}
Can anyone see why I am not getting a match?
谁能明白为什么我没有得到匹配?
I also tried type-casting test to an integer with:
我还尝试对整数进行类型转换测试:
test = Number(test);
...but this doesn't work as it has to be a String to support the .match method.
...但这不起作用,因为它必须是一个字符串才能支持 .match 方法。
回答by Matt Ball
You're mixing the two different regex constructors. Pick one:
您正在混合两种不同的正则表达式构造函数。选一个:
var pattern = new RegExp("^\d{9}$");
// or
var pattern = /^\d{9}$/;
N.B.you probably want to add start and end anchors (as above) to make sure the wholestring matches.
注意您可能想要添加开始和结束锚点(如上)以确保整个字符串匹配。
回答by vcsjones
You are mixing regex literal syntaxwith the RegExp class. Try:
您正在将regex 文字语法与 RegExp 类混合。尝试:
var test = "123123123";
var pattern = /\d{9}/;
if(test.match(pattern))
{
alert('match!');
//This code never executes
}
else
{
//This code is always executing
alert('no match!');
}?
Additionally, you may want to change your regex to match on the beginning and end of line if you want to ensure it contains onlya 9 digit number. Right now the regex will match any string that contains a 9 digit number. You would change it like so:
此外,你可能想改变你的正则表达式匹配的开始,如果你想确保它包含了行结束只有9位数字。现在正则表达式将匹配任何包含 9 位数字的字符串。你会像这样改变它:
var pattern = /^\d{9}$/;
回答by J?rg W Mittag
You aren't looking for 9 consecutive digits, you are looking for 9 consecutive digits enclosed in slashes.
您不是在寻找 9 个连续数字,而是在寻找包含在 slashes 中的9 个连续数字。
回答by Michael Berkowski
When using a new Regexp()
constructor, you should not include the /
delimieters, since they are treated as literals in the expression.
使用new Regexp()
构造函数时,不应包含/
分隔符,因为它们在表达式中被视为文字。
Also your expression should be anchored ^$
to ensure it contains no other characters than the nine digits.
此外,您的表达式应该被锚定^$
以确保它不包含九位数字以外的其他字符。
var test = "123123123";
// \d would need to be escaped as \d in RegExp()
var pattern = new RegExp("^\d{9}$");
// ..Or...
var pattern = new RegExp("^[0-9]{9}$");
Using a regular expression literal without new Regexp()
is often a better strategy if you don't need to do string manipulations or include variables in the regex, since it avoids the extra backslash-escaping.
new Regexp()
如果您不需要进行字符串操作或在正则表达式中包含变量,则使用正则表达式文字通常是更好的策略,因为它避免了额外的反斜杠转义。
var pattern = /^\d{9}$/;
回答by Stefan Seemayer
Try removing the slashes and escaping the backslash:
尝试删除斜杠并转义反斜杠:
var test = "123123123";
var pattern = new RegExp("\d{9}");
if(test.match(pattern))
{
//This code never executes
alert("match");
}
else
{
//This code is always executing
alert('no match!');
}