regex - Javascript Regular Expression detect all the Phone Number from the page source -
can me figure out how this? current 1 (i find 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 me this? 1 regular expression can detect of these kind of phone number or @ least can use 2-3 regular expressions detect them.
your regex goes through lot of trouble make sure country code matches strict set of rules seems not care follows it. means following examples matched it:
+86-0000 +86----0 +86-1-1-1-1
the following regex shorter, not strict on country code strict on overall structure of phone number.
(?:\+?(\d{1,3}))?[- (]*(\d{3})[- )]*(\d{3})[- ]*(\d{4})(?: *x(\d+))?\b
it not match examples above , 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 these examples, capture groups contain 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)
Comments
Post a Comment