Regex for email addresses in JavaScript
A pattern worth using, what it deliberately does not catch, and why the RFC-complete version is the wrong goal.
Frequently asked questions
What is the best regex for validating an email address?
A deliberately loose one: /^[^\s@]+@[^\s@]+\.[^\s@]+$/. It catches the mistakes people actually make — a missing @, a typo, a pasted sentence — without rejecting valid addresses it does not recognise. Tightening it beyond this trades a small gain in rejected typos for a real risk of rejecting real customers.
Why not use a fully RFC 5322 compliant regex?
Because it is around 6,000 characters, unmaintainable, and still wrong: it accepts addresses no mail server will deliver to, and it does not accept internationalised addresses. It solves a problem you do not have. The question you actually need answered is "will mail reach this address", and no regex can answer that.
How do I actually verify an email address?
Send an email to it with a confirmation link. That is the only method that establishes both that the address exists and that the person entering it can receive mail there. Everything before that step — regex, DNS MX lookup, disposable-domain lists — is about catching typos early, not about verification.
Should I use type="email" instead?
Use it as well. The browser gives you a free first pass, a sensible mobile keyboard, and accessible error messaging. Its own check is roughly as loose as the pattern above, which is a deliberate choice by the HTML specification for the same reasons. Always re-check server-side: client-side validation is a usability feature, not a security control.
Do I need to worry about ReDoS in an email regex?
Yes, if your pattern nests quantifiers. Several widely-copied "RFC compliant" email regexes contain constructs like (a+)+ that take exponential time on a crafted input, and the input here is by definition attacker-supplied. The loose pattern above has no nesting and cannot backtrack pathologically.
Found a mistake on this page? Tell me — a page that is confidently wrong is worse than no page.