有些人可以回答我对regexp做错了什么。我需要编写regexp,它只能通过这三种格式传递来自phonenumber的值: “+38(093)937-99-92”和“093 937 99 92”和“(093)937 99 92”
在我开始写这件事的过程中我发现了一个错误example of error and how i do
我知道逃避符号但在我的例子中我逃脱的所有符号,其他都是正则表达式的一部分,因为我知道......
$.validator.addMethod("phone", function(value, element) {
return /^\+[0-9]{2}\([0-9]{3}\)[0-9]{3}+$/i.test(value);
}
答案 0 :(得分:1)
^(?:\+[0-9]{2}\s)?(?:\([0-9]{3}\)|[0-9]{3})\s[0-9]{3}(-|\s)[0-9]{2}\1[0-9]{2}
此正则表达式将匹配以下格式的字符串
现场演示
https://regex101.com/r/lM8hS0/1
NODE EXPLANATION
----------------------------------------------------------------------
^ the beginning of a "line"
----------------------------------------------------------------------
(?: group, but do not capture (optional
(matching the most amount possible)):
----------------------------------------------------------------------
\+ '+'
----------------------------------------------------------------------
[0-9]{2} any character of: '0' to '9' (2 times)
----------------------------------------------------------------------
\s whitespace (\n, \r, \t, \f, and " ")
----------------------------------------------------------------------
)? end of grouping
----------------------------------------------------------------------
(?: group, but do not capture:
----------------------------------------------------------------------
\( '('
----------------------------------------------------------------------
[0-9]{3} any character of: '0' to '9' (3 times)
----------------------------------------------------------------------
\) ')'
----------------------------------------------------------------------
| OR
----------------------------------------------------------------------
[0-9]{3} any character of: '0' to '9' (3 times)
----------------------------------------------------------------------
) end of grouping
----------------------------------------------------------------------
\s whitespace (\n, \r, \t, \f, and " ")
----------------------------------------------------------------------
[0-9]{3} any character of: '0' to '9' (3 times)
----------------------------------------------------------------------
( group and capture to \1:
----------------------------------------------------------------------
- '-'
----------------------------------------------------------------------
| OR
----------------------------------------------------------------------
\s whitespace (\n, \r, \t, \f, and " ")
----------------------------------------------------------------------
) end of \1
----------------------------------------------------------------------
[0-9]{2} any character of: '0' to '9' (2 times)
----------------------------------------------------------------------
\1 what was matched by capture \1
----------------------------------------------------------------------
[0-9]{2} any character of: '0' to '9' (2 times)
----------------------------------------------------------------------
答案 1 :(得分:0)
这是问题的尾随+
。你需要重复一些事情,例如:(?:[0-9]{3})+
。但是这个正则表达式应该符合你的需求:
/^(?:\+\d{2} \(\d{3}\) \d{3}-\d{2}-\d{2}|(?:\(\d{3}\)|\d{3}) \d{3} \d{2} \d{2})$/
我删除了链接页面中的^
和$
(字符串的开头和结尾),还添加了g
lobal标志,以便您可以看到它是如何工作的。