我想要一个用于手机号码验证的正则表达式.正则表达式应该是这样的,它必须+
只在开始时才接受,并且-
只有在国家代码之后才允许空格(或)(仅一次).国家代码后只允许10位数字.国家代码应该是可选的.如果是国家代码不存在,它应该只接受10位数字.Regex应该防止任何无效的数字,如(例如:+91 0000000000
或0000000000
).
正则表达式应该接受像这样的数字
__PRE__
Gau*_*don 45
如果您使用下面介绍的技巧,请满足您的所有要求
/^(\+\d{1,3}[- ]?)?\d{10}$/
^
行的开始+
后跟\d+
一个
or -
哪个是可选的.0
不遵循.\d+
10次.DEMO 在演示中添加了m
ultiline标志以检查所有情况
PS你真的需要指定你使用的语言,以便使用if
下面的条件:
// true if above regex is satisfied and (&&) it does not (`!`) match `0`s `5` or more times
if(number.match(/^(\+\d{1,3}[- ]?)?\d{10}$/) && ! (number.match(/0{5,}/)) )
Run Code Online (Sandbox Code Playgroud)
小智 8
这个正则表达式非常简短而且适合工作。
/^([+]\d{2})?\d{10}$/
例如:+910123456789 或 0123456789
-> /^ and $/ is for starting and ending
-> The ? mark is used for conditional formatting where before question mark is available or not it will work
-> ([+]\d{2}) this indicates that the + sign with two digits '\d{2}' here you can place digit as per country
-> after the ? mark '\d{10}' this says that the digits must be 10 of length change as per your country mobile number length
这就是这个手机号码正则表达式的工作方式。
+ 符号用于数字的全球匹配。
如果你想在中间添加空格,那么你可以使用
[ ]
这里方括号表示字符序列,空格是用于在正则表达式中搜索的字符。
对于空格分隔的数字,您可以使用此正则表达式
/^([+]\d{2}[ ])?\d{10}$/
例如:+91 0123456789
谢谢,如果你有任何问题。
试试这个正则表达式:
^(\+?\d{1,4}[\s-])?(?!0+\s+,?$)\d{10}\s*,?$
Run Code Online (Sandbox Code Playgroud)
使用Perl的YAPE对正则表达式的解释如下:
NODE EXPLANATION
----------------------------------------------------------------------
(?-imsx: group, but do not capture (case-sensitive)
(with ^ and $ matching normally) (with . not
matching \n) (matching whitespace and #
normally):
----------------------------------------------------------------------
^ the beginning of the string
----------------------------------------------------------------------
( group and capture to \1 (optional
(matching the most amount possible)):
----------------------------------------------------------------------
\+? '+' (optional (matching the most amount
possible))
----------------------------------------------------------------------
\d{1,4} digits (0-9) (between 1 and 4 times
(matching the most amount possible))
----------------------------------------------------------------------
[\s-] any character of: whitespace (\n, \r,
\t, \f, and " "), '-'
----------------------------------------------------------------------
)? end of \1 (NOTE: because you are using a
quantifier on this capture, only the LAST
repetition of the captured pattern will be
stored in \1)
----------------------------------------------------------------------
(?! look ahead to see if there is not:
----------------------------------------------------------------------
0+ '0' (1 or more times (matching the most
amount possible))
----------------------------------------------------------------------
\s+ whitespace (\n, \r, \t, \f, and " ") (1
or more times (matching the most amount
possible))
----------------------------------------------------------------------
,? ',' (optional (matching the most amount
possible))
----------------------------------------------------------------------
$ before an optional \n, and the end of
the string
----------------------------------------------------------------------
) end of look-ahead
----------------------------------------------------------------------
\d{10} digits (0-9) (10 times)
----------------------------------------------------------------------
\s* whitespace (\n, \r, \t, \f, and " ") (0 or
more times (matching the most amount
possible))
----------------------------------------------------------------------
,? ',' (optional (matching the most amount
possible))
----------------------------------------------------------------------
$ before an optional \n, and the end of the
string
----------------------------------------------------------------------
) end of grouping
----------------------------------------------------------------------
Run Code Online (Sandbox Code Playgroud)