我需要一个正则表达式,其中任何数字都允许使用任何顺序的空格,括号和连字符。但必须最后是“+”(加号)。
答案 0 :(得分:14)
您可以使用正则表达式:
^[\d() -]+\+$
说明:
^ : Start anchor
[ : Start of char class.
\d : Any digit
( : A literal (. No need to escape it as it is non-special inside char class.
) : A literal )
: A space
- : A hyphen. To list a literal hyphen in char class place it at the beginning
or at the end without escaping it or escape it and place it anywhere.
] : End of char class
+ : One or more of the char listed in the char class.
\+ : A literal +. Since a + is metacharacter we need to escape it.
$ : End anchor
答案 1 :(得分:1)
如果规则意味着整个字符串必须符合它们,那么:
/^[\d\(\)\- ]+\+$/
这将匹配(i)435 (345-325) +
但不匹配(ii)my phone is 435 (345-325)+, remember it
。
如果您想从(ii)中提取(i),您可以使用我原来的RegExp:
/[\d\(\)\- ]+\+/