extract-email-js
v1.0.2
Published
Extract email addresses from text, including obfuscated and tagged emails
Readme
extract-email-js
A lightweight JavaScript library for extracting email addresses from text, including support for obfuscated and tagged emails.
Features
- Extracts email addresses from plain text
- Handles obfuscated emails (e.g.,
foo [at] bar [dot] com) - Removes spaces in emails (e.g.,
f o o @ b a r . c o m→[email protected]) - Filters out invalid top-level domains (TLDs)
- Supports email tags (e.g.,
[email protected]) - Works in both Node.js and browser environments
Installation
Using npm
npm install extract-email-jsUsing yarn
yarn add extract-email-jsDirectly in browser
<script type="module">
import { extractEmail } from 'https://unpkg.com/extract-email-js/dist/index.esm.js';
// Your code here
</script>Usage
Basic Usage
import { extractEmail } from 'extract-email-js';
// for commonjs
const extractEmail = require('extract-email-js');
const text = 'Contact us at [email protected] or sales [at] example [dot] com';
const emails = extractEmail(text);
console.log(emails);
// Output: [
// { email: '[email protected]' },
// { email: '[email protected]' }
// ]Handling Results
The function returns an array of objects, where each object contains an email property with the extracted email address.
const text = 'Emails: [email protected], [email protected]';
const emails = extractEmail(text);
emails.forEach(({ email }) => {
console.log(`Found email: ${email}`);
});Test Cases
The library includes the following test cases:
Basic Email Extraction
- Input:
'Contact me at [email protected]' - Output:
[{ email: '[email protected]' }]
- Input:
Multiple Emails
- Input:
'Emails: [email protected], [email protected]' - Output:
[{ email: '[email protected]' }, { email: '[email protected]' }]
- Input:
Obfuscated Emails
- Input:
'Obfuscated: f o o @ b a r . c o m and baz [at] qux [dot] com' - Output:
[{ email: '[email protected]' }, { email: '[email protected]' }]
- Input:
Tagged Emails
- Input:
'Tagged email: [email protected]' - Output:
[{ email: '[email protected]' }]
- Input:
Emails with Special Characters
- Input:
'Surrounded by emojis 📧[email protected]' - Output:
[{ email: '[email protected]' }]
- Input:
Invalid TLDs
- Input:
'Invalid TLD: [email protected]' - Output:
[]
- Input:
Invalid Email Format
- Input:
'Invalid email: foo@bar' - Output:
[]
- Input:
Supported TLDs
The library validates emails against the following top-level domains (TLDs):
- .com
- .org
- .net
- .io
- .edu
- .gov
Building from Source
Clone the repository:
git clone https://github.com/yourusername/extract-email-js.git cd extract-email-jsInstall dependencies:
npm installRun tests:
npm test
License
MIT © Gauransh Juneja
