简体   繁体   English

电话号码的 Javascript 正则表达式

[英]Javascript regex for Phone Number

I did the work and wrote the following regex:我完成了这项工作并编写了以下正则表达式:

/^([0-9.]+)$/ /^([0-9.]+)$/

This is satisfying for the following conditions:这满足以下条件:

123.123.123.132
123123213123

Now I need add one more feature for this regex that it can have one alphabet in the Phone Number like现在我需要为这个正则表达式再添加一项功能,它可以在电话号码中包含一个字母,例如

123.a123.b123.123

but not但不是

123.aa1.bb12

I tried with我试过

/^([0-9.]+\\w{1})$/ /^([0-9.]+\\w{1})$/

It can contain only one alphabet between the .(dot) symbol.它只能在 .(点)符号之间包含一个字母。 Can someone help me on this !!!有人可以帮我解决这个问题吗!!!

Thanks in Advance !!提前致谢 !!

The pattern that you use ^([0-9.]+)$ uses a character class which will match any of the listed characters and repeats that 1+ times which will match for example 123.123.123.132 .您使用的模式^([0-9.]+)$使用的字符类将匹配任何列出的字符,并重复 1+ 次将匹配例如123.123.123.132

This is a bit of a broad match and does not take into account the position of the matched characters.这有点广泛匹配,没有考虑匹配字符的位置。

If your values starts with 1+ digits and the optional az can be right after the dot, you might use:如果您的值以 1+ 位数字开头并且可选的 az 可以紧跟在点之后,您可以使用:

^\d+(?:\.[a-zA-Z]?\d+)*$

Explanation解释

  • ^ Start of the string ^字符串的开始
  • \\d+ Match 1+ digits \\d+匹配 1+ 个数字
  • (?: Non capturing group (?:非捕获组
    • \\.[a-zA-Z]?\\d+ Match a dot followed by an optional a-zA-Z and 1+ digits \\.[a-zA-Z]?\\d+匹配一个点,后跟一个可选的 a-zA-Z 和 1+ 个数字
  • )* Close group and repeat 0+ times )*关闭组并重复 0+ 次
  • $ End of the string $字符串结尾

See the regex101 demo参见regex101 演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM