Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/55.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jquery 简单电话号码正则表达式不工作_Jquery_Regex - Fatal编程技术网

Jquery 简单电话号码正则表达式不工作

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

我的数据:(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-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

您会很容易看到这个问题。

非常感谢您的详细解释!