256 Kilobytes

[Tool + Code] Email Address and Phone Number Extractor: Using JavaScript Code to Extract Emails and Phone Numbers with a Regular Expressions

Articles in Web Scraping, Data Analysis | By August R. Garcia

Published 1 month agoTue, 15 Jan 2019 14:46:28 -0800 | Last update 3 weeks agoSun, 27 Jan 2019 12:12:57 -0800

Sometimes, you have to extract emails and/or phone numbers from some text. Here's a tool that does that.

133 views, 1 RAMs, and 0 comments

[Tool] Email Address and Phone Number Extractor

Also, JavaScript Code to Extract Emails and Phone Numbers with a REGEX

Sometimes, you have to extract emails and/or phone numbers from some text. Here's a tool that does that.

Or, scroll down to get the JavaScript code to match/extract emails and/or phone numbers from arbitrary contexts. All code is free to use in any context, including commercial contexts, with no requirement for attribution (i.e., do whatever the fuck you want).

What is a RegEx?

Regular expressions are patterns that can be used to find, replace, extract, and/or otherwise match parts of text. They are similar to something like Ctrl+F, but can be designed to match strings at much higher level of specificity.

While most programming languages, spreadsheet applications, database management systems, and so on will support regular expressions, they are a general standard that is not specific to any particular programming language. Exact implementations can vary between programming languages and standards, although regular expressions are, somehow, basically a consistent standard.

https://www.youtube.com/watch?v=sa-TUpSx1JA

The video above overviews the general concepts behind regular expressions fairly concisely and in a way that is relatively easy to follow with no prior knowledge of RegExes.

[JavaScript] Extracting Data by RegEx

How to Extract Email Addresses

[JavaScript] RegEx to Match Email Addresses

function extract_emails( text ) {
     var regex = /(?:^|\s|$)([a-zA-Z0-9.\+_-]+@[a-zA-Z0-9._-]+)/gi;
     var matches = []; var match;
     while (match = regex.exec(text)) { matches.push(match[1]); }
     return matches; 
}

Matches These Email Addresses

The email RegEx has been tested against the list of examples of valid and invalid email addresses listed on Wikipedia.

  • simple@example.com
  • very.common@example.com
  • disposable.style.email.with+symbol@example.com
  • other.email-with-hyphen@example.com
  • fully-qualified-domain@example.com
  • user.name+tag+sorting@example.com
  • x@example.com
  • example-indeed@strange-example.com
  • admin@mailserver1
  • example@s.example
  • " "@example.org
  • "john..doe"@example.org

Does not match these invalid email addresses :

  • Abc.example.com
  • A@b@c@example.com
  • a"b(c)d,e:f;g<h>i[j\k]l@example.com
  • just"not"right@example.com
  • this is"not\allowed@example.com
  • this\ still\"not\\allowed@example.com
  • 1234567890123456789012345678901234567890123456789012345678901234+x@example.com

How to Extract Phone Numbers

[JavaScript] RegEx to Match Phone Numbers

function extract_phone_numbers( text ) {     return text.match(/\+?[0-9]?[- \.]?[(]?[0-9]{3}[)]?[-\s\.]?[0-9]{3}[-\s\.]?[0-9]{4,6}/ig); }

Matches These Phone Numbers

This RegEx matches phone numbers using these types of patterns:

  • 5555555555
  • 555 555 5555
  • 555-555-5555
  • 555.555.5555
  • (555) 555-5555
  • 15555555555
  • 1 555 555 5555
  • 1 555-555-5555
  • 1 555.555.5555
  • 1 (555) 555-5555
  • +15555555555
  • +1 555 555 5555
  • +1 555-555-5555
  • +1 555.555.5555
  • +1 (555) 555-5555

In Conclusion

Are there other pieces of data that you commonly need to extract? Post a REGEX in the comments and will duct tape it into the tool above. Or post a suggestion and someone more competent than you might even implement it for you.

Or, if this site is too shitty for you, go to some other website, like RegExLib[rary], which has various RegExes, including for matching emails and phone numbers. Or, alternatively, go to a tool like Regex101 to write and/or test your own RegEx.

Users Who Have Downloaded More RAM:
Huevos Rancheros (1 month ago)
🐏 ⨉ 1
Posted by August R. Garcia 1 month ago

Edit History

• [2019-01-15 14:46 PST] August R. Garcia (1 month ago)
• [2019-01-15 14:46 PST] August R. Garcia (1 month ago)
• [2019-01-15 14:46 PST] August R. Garcia (1 month ago)
• [2019-01-15 14:46 PST] August R. Garcia (1 month ago)
• [2019-01-15 14:46 PST] August R. Garcia (1 month ago)
🕓 Posted at 15 January, 2019 14:46 PM PST
Profile Photo - August R. Garcia August R. Garcia Posting on Internet... Portland, OR
🗎 60 🗨 481 🐏 86
Site Owner

Grahew Mattham

August Garcia is some guy who used to sell Viagra on the Internet. He made this website to LARP as a sysadmin while posting about garbage like user-agent spoofing, spintax, the only good keyboard, virtual assitants from Pakistan, links with the rel="nofollow" attributeproxies, regular expressions, sin, and probably some other trash.


Account created 2 months ago.
60 posts, 481 comments, and 86 RAMs.

Last active 1 day ago:
Commented in thread [Master Thread] Notifications for Scheduled Downtime

Post a New Comment

To leave a comment, login to your account or create an account.