Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript Regular Expression detect all the Phone Number from the page source

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.

like image 427
Ddo Avatar asked Sep 02 '25 17:09

Ddo


2 Answers

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.

like image 129
HamZa Avatar answered Sep 04 '25 06:09

HamZa


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)
like image 26
Francis Gagnon Avatar answered Sep 04 '25 08:09

Francis Gagnon