结合正则表达式来验证英国和美国的电话号码 [英] Combining regex to validate UK and US phone number
问题描述
我有两个正则表达式.一个验证英国号码(来自我的上一个问题)和另一个验证美国号码:
I have two regex. One to validate UK number (from my previous question) and another to validate US number:
^\(?(?:(?:0(?:0|11)\)?[\s-]?\(?|\+)44\)?[\s-]?\(?(?:0\)?[\s-]?\(?)?|0)(?:\d{2}\)?[\s-]?\d{4}[\s-]?\d{4}|\d{3}\)?[\s-]?\d{3}[\s-]?\d{3,4}|\d{4}\)?[\s-]?(?:\d{5}|\d{3}[\s-]?\d{3})|\d{5}\)?[\s-]?\d{4,5}|8(?:00[\s-]?11[\s-]?11|45[\s-]?46[\s-]?4\d))(?:(?:[\s-]?(?:x|ext\.?\s?|\#)\d+)?)$^
/\(?[2-9][0-8][0-9]\)?[-.]?[0-9]{3}[-.]?[0-9]{4}/
我想将这两者结合起来,以便我可以验证任何美国或英国号码.
I want to combine both of this so that I can validate any US or UK number.
或者 |
操作就够了吗?
Does or |
operation suffice for this?
这里我使用 | 将它们结合起来但它不验证数字:
Here I combined both of them using | but it does not validate the number:
推荐答案
是的,您的 |
运算符应该可以解决这个问题.它看起来像这样:
Yes, your |
operator should be fine for that. It would look like this:
/((^\(?(?:(?:0(?:0|11)\)?[\s-]?\(?|\+)44\)?[\s-]?\(?(?:0\)?[\s-]?\(?)?|0)(?:\d{2}\)?[\s-]?\d{4}[\s-]?\d{4}|\d{3}\)?[\s-]?\d{3}[\s-]?\d{3,4}|\d{4}\)?[\s-]?(?:\d{5}|\d{3}[\s-]?\d{3})|\d{5}\)?[\s-]?\d{4,5}|8(?:00[\s-]?11[\s-]?11|45[\s-]?46[\s-]?4\d))(?:(?:[\s-]?(?:x|ext\.?\s?|\#)\d+)?)$)|(\(?[2-9][0-8][0-9]\)?[-. ]?[0-9]{3}[-. ]?[0-9]{4}))/
它适用于您问题中的正则表达式演示:http://regex101.com/r/zX2cE5
It works for the regex demo in your question: http://regex101.com/r/zX2cE5
这篇关于结合正则表达式来验证英国和美国的电话号码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!