我想为一个标准的美国类型的电话号码,支持以下格式写一个正则表达式:

###-###-####
(###) ###-####
### ### ####
###.###.####

其中#表示任意数字。到目前为止,我想出了以下表达方式

^[1-9]\d{2}-\d{3}-\d{4}
^\(\d{3}\)\s\d{3}-\d{4}
^[1-9]\d{2}\s\d{3}\s\d{4}
^[1-9]\d{2}\.\d{3}\.\d{4}

分别。我不太确定最后一条是否正确。我还想知道是否有任何方式,我可以写一个单一的表达式,而不是4个不同的迎合我提到的不同格式。如果是这样,我不知道该怎么做。以及我如何修改表达式/表达式,以便我也可以包括一个条件,以支持区号作为可选组件。类似的

+1 ### ### ####

其中+1是区号,是可选的。


当前回答

也许这是最简单的方法。

\(?\d+\)?[-.\s]?\d+[-.\s]?\d+

匹配如下:

(555) 444-6789 The 555-444-6789 555.444.6789 555 444 6789

其他回答

我使用的电话号码正则表达式: / ^ [+] ? (\ d {1,2}) ? [\ s -] ? \ (\ d {3} \) ? [\ s -] ? \ d {3} [\ s -) ?\ d{4} /美元

封面:

18001234567 1 800 123 4567 Plus 1 800 123-4567 +86 800 123 4567 800-123-4567 1 (800) 123-4567 (800)123-4567 (800) 123-4567 (800)1234567 The 800-123-4567 800.123.4567

从@Ravi的回答开始,我还为NPA(地区)代码应用了一些验证规则。

特别是:

它应该以2(或更高)开头 第二和第三位数字不能是“11”(N11)。

还有一些其他限制,包括保留块(N9X, 37X, 96X)和555,但我省略了这些,特别是因为保留块将来可能会用到,而555对于测试很有用。

这是我想到的:

^((\+\d{1,2}|1)[\s.-]?)?\(?[2-9](?!11)\d{2}\)?[\s.-]?\d{3}[\s.-]?\d{4}$

或者,如果你也想匹配空白值(如果该字段不是必需的),你可以使用:

(^((\+\d{1,2}|1)[\s.-]?)?\(?[2-9](?!11)\d{2}\)?[\s.-]?\d{3}[\s.-]?\d{4}$|^$)

我对有效数字的测试用例(许多来自@Francis的答案)是:

18005551234
1 800 555 1234
+1 800 555-1234
+86 800 555 1234
1-800-555-1234
1.800.555.1234
+1.800.555.1234
1 (800) 555-1234
(800)555-1234
(800) 555-1234
(800)5551234
800-555-1234
800.555.1234

我的无效测试用例包括:

(003) 555-1212     // Area code starts with 0
(103) 555-1212     // Area code starts with 1
(911) 555-1212     // Area code ends with 11
180055512345       // Too many digits
1 800 5555 1234    // Prefix code too long
+1 800 555x1234    // Invalid delimiter
+867 800 555 1234  // Country code too long
1-800-555-1234p    // Invalid character
1 (800)  555-1234  // Too many spaces
800x555x1234       // Invalid delimiter
86 800 555 1212    // Non-NA country code doesn't have +

我的正则表达式不包括分组来提取数字组,但可以修改它以包括这些数字组。

这个问题有很多可能的变化。这是一个正则表达式,类似于我之前放在SO上的答案。

^\s*(?:\+?(\d{1,3}))?[-. (]*(\d{3})[-. )]*(\d{3})[-. ]*(\d{4})(?: *x(\d+))?\s*$

它将匹配以下示例和更多:

18005551234
1 800 555 1234
+1 800 555-1234
+86 800 555 1234
1-800-555-1234
1 (800) 555-1234
(800)555-1234
(800) 555-1234
(800)5551234
800-555-1234
800.555.1234
800 555 1234x5678
8005551234 x5678
1    800    555-1234
1----800----555-1234

无论电话号码的输入方式如何,都可以使用捕获组来分解电话号码,以便在代码中处理它。

第一组:国家代码(例如:1或86) 第二组:地区号码(例如:800) 分组3:Exchange(例如:555) 组四:用户号码(例如:1234) 组5:分机(例如:5678)

如果你感兴趣的话,下面是这个表达的分类:

^\s*                #Line start, match any whitespaces at the beginning if any.
(?:\+?(\d{1,3}))?   #GROUP 1: The country code. Optional.
[-. (]*             #Allow certain non numeric characters that may appear between the Country Code and the Area Code.
(\d{3})             #GROUP 2: The Area Code. Required.
[-. )]*             #Allow certain non numeric characters that may appear between the Area Code and the Exchange number.
(\d{3})             #GROUP 3: The Exchange number. Required.
[-. ]*              #Allow certain non numeric characters that may appear between the Exchange number and the Subscriber number.
(\d{4})             #Group 4: The Subscriber Number. Required.
(?: *x(\d+))?       #Group 5: The Extension number. Optional.
\s*$                #Match any ending whitespaces if any and the end of string.

要使区号可选,只需在区号(\d{3})后面添加一个问号。

用“+”分隔多个数字,然后用“;”"," -"或" "字符?

在jsfiddle上使用上述解决方案添加一个示例。 我根据客户的要求修改了代码。希望这也能帮助到一些人。

/^\s*(?:\+?(\d{1,3}))?[- (]*(\d{3})[- )]*(\d{3})[- ]*(\d{4})(?: *[x/#]{1}(\d+))?\s*$/

请看这里的例子