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 3 months agoTue, 15 Jan 2019 14:46:28 -0800 | Last update 2 months agoTue, 19 Feb 2019 02:03:28 -0800

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

339 views, 1 RAM, 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.

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 (3 months ago)
🐏 ⨉ 1
Posted by August R. Garcia 3 months ago

Edit History

• [2019-01-15 14:46 PST] August R. Garcia (3 months ago)
• [2019-01-15 14:46 PST] August R. Garcia (3 months ago)
• [2019-01-15 14:46 PST] August R. Garcia (3 months ago)
• [2019-01-15 14:46 PST] August R. Garcia (3 months ago)
• [2019-01-15 14:46 PST] August R. Garcia (3 months ago)
• [2019-01-15 14:46 PST] August R. Garcia (3 months ago)
🕓 Posted at 15 January, 2019 14:46 PM PST
Profile Photo - August R. Garcia August R. Garcia LARPing as a Sysadmi... Portland, OR
🗎 106 🗨 623 🐏 117
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, HTML and CSSsin, the developer console, and probably some other trash.


Account created 4 months ago.
106 posts, 623 comments, and 117 RAMs.

Last active 18 hours ago:
Commented in thread Quality Content Dump: The Thread

Post a New Comment

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

Do you like having a good time?

Read Quality Articles

Read some quality articles. If you can manage to not get banned for like five minutes, you can even post your own articles.

View Articles →

Argue with People on the Internet

Use your account to explain why people are wrong on the Internet forum.

View Forum →

Vandalize the Wiki

Or don't. I'm not your dad.

View Wiki →

Ask and/or Answer Questions

If someone asks a terrible question, post a LMGTFY link.

View Answers →

Make Some Money

Hire freelancers and/or advertise your goods and/or services. Hire people directly. We're not a middleman or your dad. Manage your own business transactions.

Register an Account
You can also login to an existing account or recover your password. All use of this site is subject to terms outlined in the terms of service and privacy policy.