我想为一个标准的美国类型的电话号码,支持以下格式写一个正则表达式:
###-###-####
(###) ###-####
### ### ####
###.###.####
其中#表示任意数字。到目前为止,我想出了以下表达方式
^[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是区号,是可选的。
这个问题有很多可能的变化。这是一个正则表达式,类似于我之前放在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})后面添加一个问号。
我知道这不能直接回答OP的问题,但如果您问的是与OP相同的问题,那么很有可能您正在寻找一种方法来验证和存储电话号码在状态或数据库中。与其试图检测每个可能是电话号码的字符组合,不如将此任务分解为多个步骤。
去掉所有的无数字
去掉前导1s
请确保号码不超过10位
Javascript伪例子,假设"phone"是用户输入存储为字符串:
phone.replace(/\D/g, "")
phone.replace(/^1+/g, "")
phone.slice(0, 10)
phone.length === 10 ? "do something" : "don't do something"
上面的代码将需要根据您的目的进行调整,并为非javascript读者保留尽可能简单的代码。
为了表示的目的,你可以总是层破折号和领先的1,但为了存储,你应该只保留实际的数字。这种方法还有一个额外的优点,就是留给您一些易于理解的正则表达式。