Published on

How to check or extract Email Addresses in Javascript

Authors

At one point or another, we might have come across the case in which we need to do a proper email validation or extract email address from a string.

For our case, we'll be using a tiny npm package email-regex

Install the Dependencies

npm install email-regex

This package is just a helper function for the email regex - nothing more, nothing less

Snippets

import emailRegex from 'email-regex';

// Check if the string contains email address
emailRegex().test('hello hey@example.com'); // true

// Check if has only email string
emailRegex({exact: true}).test('hello hey@example.com'); // false

// Extract email from a string
'hello hey@example.com hi@bye.com'.match(emailRegex()); 
// ['hey@example.com', 'hi@bye.com']

Happy extracting email-addresses!