admin管理员组文章数量:1353630
Can anyone help me figure out how to do this? My current one is (I find it on somewhere):
/(\+(9[976]\d|8[987530]\d|6[987]\d|5[90]\d|42\d|3[875]\d|2[98654321]\d|9[8543210]|8[6421]|6[6543210]|5[87654321]|4[987654310]|3[9643210]|2[70]|7)[0-9. -]{4,14})(?:\b|x\d+)/
It can detect: +86-400-660-8680
But not this:
- +1 888 204 3539
- 1-800-667-6389
- +1-400-660-8680
- (877) 359-6695
- 800-692-7753
Can you help me with this? 1 Regular Expression can detect all of these kind of phone number or at least I can use 2-3 Regular Expressions to detect them.
Can anyone help me figure out how to do this? My current one is (I find it on somewhere):
/(\+(9[976]\d|8[987530]\d|6[987]\d|5[90]\d|42\d|3[875]\d|2[98654321]\d|9[8543210]|8[6421]|6[6543210]|5[87654321]|4[987654310]|3[9643210]|2[70]|7)[0-9. -]{4,14})(?:\b|x\d+)/
It can detect: +86-400-660-8680
But not this:
- +1 888 204 3539
- 1-800-667-6389
- +1-400-660-8680
- (877) 359-6695
- 800-692-7753
Can you help me with this? 1 Regular Expression can detect all of these kind of phone number or at least I can use 2-3 Regular Expressions to detect them.
Share Improve this question edited Oct 3, 2015 at 6:12 anonameuser22 10813 bronze badges asked May 19, 2013 at 4:51 DdoDdo 3953 silver badges12 bronze badges 1- 2 Could you list out every type of phone number that you need found? Also what are you doing with them? – Qantas 94 Heavy Commented May 19, 2013 at 5:45
2 Answers
Reset to default 6So here's the huge regex that will match your needs:
(+?(?:(?:9[976]\d|8[987530]\d|6[987]\d|5[90]\d|42\d|3[875]\d|2[98654321]\d|9[8543210]|8[6421]|6[6543210]|5[87654321]|4[987654310]|3[9643210]|2[70]|7|1)|((?:9[976]\d|8[987530]\d|6[987]\d|5[90]\d|42\d|3[875]\d|2[98654321]\d|9[8543210]|8[6421]|6[6543210]|5[87654321]|4[987654310]|3[9643210]|2[70]|7|1)))[0-9. -]{4,14})(?:\b|x\d+)
Online demo
And here's how I did to make it.
Your regex goes through a lot of trouble to make sure the country code matches a strict set of rules but seems to not care what follows it. Which means the following examples would be matched by it:
+86-0000
+86----0
+86-1-1-1-1
The following regex is much shorter, is not as strict on the country code but is strict on the overall structure of the phone number.
(?:\+?(\d{1,3}))?[- (]*(\d{3})[- )]*(\d{3})[- ]*(\d{4})(?: *x(\d+))?\b
It would not match the examples above and would match these examples:
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 1234x5678
8005551234 x5678
1 800 555-5555
1----800----555-5555
For all these examples, the capture groups would contain the following values:
- Group1: Country Code (ex: 1 or 86)
- Group2: Area Code (ex: 800)
- Group3: Exchange (ex: 555)
- Group4: Subscriber Number (ex: 1234)
- Group5: Extension (ex: 5678)
本文标签: regexJavascript Regular Expression detect all the Phone Number from the page sourceStack Overflow
版权声明:本文标题:regex - Javascript Regular Expression detect all the Phone Number from the page source - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1743846336a2549188.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论