Jquery 简单电话号码正则表达式不工作
我的数据:(222)222-2222 为什么这还没有过去Jquery 简单电话号码正则表达式不工作,jquery,regex,Jquery,Regex,我的数据:(222)222-2222 为什么这还没有过去 checkRegexp(TelephoneNumber, /^(d{3}) d{3}-d{4}$/, "Please enter your 10 digit phone number."); function checkRegexp(o, regexp, n) { if (!(regexp.test(o.val()))) { o.addClass("ui-state-er
checkRegexp(TelephoneNumber, /^(d{3}) d{3}-d{4}$/, "Please enter your 10 digit phone number.");
function checkRegexp(o, regexp, n) {
if (!(regexp.test(o.val()))) {
o.addClass("ui-state-error");
updateTips(n);
return false;
} else {
return true;
}
}
(
和)
是正则表达式中用于捕获组的特殊字符,您需要对它们进行转义。您还需要为数字字符设置\d
。如果没有\
,您将匹配字母d
/^\(\d{3}\) \d{3}-\d{4}$/
当您对正则表达式有问题时,您可以使用这样的网站,其中包括对正则表达式的解释。您会看到括号没有被视为文字字符
例如,您可以使用原始正则表达式:
^ assert position at start of the string
1st Capturing group (d{3})
d{3} matches the character d literally (case sensitive)
Quantifier: Exactly 3 times
matches the character literally
d{3} matches the character d literally (case sensitive)
Quantifier: Exactly 3 times
- matches the character - literally
d{4} matches the character d literally (case sensitive)
Quantifier: Exactly 4 times
$ assert position at end of the string
您会很容易看到这个问题。非常感谢您的详细解释!