Regular expression matching for email addresses. Maintained, configurable, more accurate, and browser-friendly alternative to email-regex. Works in Node v10.12.0+ and browsers. Made for Spam Scanner and Forward Email
Table of Contents
- How to validate an email address
This package should hopefully more closely resemble real-world intended usage of an email regular expression, and also let you configure several Options. Please check out Forward Email if this package helped you, and explore our source code on GitHub which shows how we use this package.
It will not perform strict email validation, but instead hints the complete matches resembling an email address. We recommend to use validator.isEmail for validation (e.g.
npm install email-regex-safe
yarn add email-regex-safe
This package automatically includes RE2 for regex optimization and vulnerability protection. You will not have to manually wrap your URL regular expressions with
new RE2(emailRegex()) anymore through
email-regex-safe (we do it automatically for you).
const emailRegexSafe = ;const str = 'some long string with firstname.lastname@example.org in it';const matches = str;for const match of matchesconsole;console;
Since RE2 is not made for the browser, it will not be used. If there were to be any regex vulnerabilities, they would only crash the user's browser tab, and not your server (as they would on the Node.js side without the use of RE2).
This is the solution for you if you're just using
<script> tags everywhere!
||Only match an exact String. Useful with
||Whether or not to abide by Gmail's rules for email usernames (see Gmail's Create a username article for more insight). Note that since RE2 does not support negative lookahead nor negative lookbehind, we are leaving it up to you to filter out a select few invalid matches while using
||Whether or not to allow UTF-8 characters for email usernames. This Boolean is only applicable if
||Allows localhost in the URL hostname portion. See the test/test.js for more insight into the localhost test and how it will return a value which may be unwanted. A pull request would be considered to resolve the "pic.jp" vs. "pic.jpg" issue.|
||Match against IPv4 URL's.|
||Match against IPv6 URL's. This is set to
||Array||tlds||Match against a specific list of tlds, or the default list provided by tlds.|
||Return the RegExp as a String instead of a
How to validate an email address
If you would like to validate email addresses found, then you should use the validator.isEmail method. This will further enforce the email RFC specification limitations of 64 characters for the username/local part of the email address, 254 for the domain/hostname portion, and 255 in total; including the "@" (at symbol).
Since we cannot use regular expression's "negative lookbehinds" functionality (due to RE2 limitations), we could not merge the logic from this pull request. This would have allowed us to make it so
example.jpeg would match only if it was
example.jp, however if you pass
example.jpeg right now it will extract
example.jp from it (since
.jp is a TLD). An alternative solution may exist, and we welcome community contributions regarding this issue.