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:
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.
So 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:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With