Match email addresses with regex
A practical regex for matching email addresses, plus the limits of regex validation, the RFC 5322 hairball, and a tested working pattern.
# pattern
/[\w.+-]+@[\w-]+\.[\w.-]+/gi → Open in regex tester (pre-filled)
# how it works
The local part allows letters, digits, dots, plus signs, and hyphens (covers Gmail "+aliases"). The domain part is letters/digits/hyphens, then a dot, then a TLD that allows multiple dots (".co.uk"). The /i flag makes it case-insensitive; /g finds every match.
# sample input
Ping [email protected] or [email protected] for the change-request. Cc [email protected] on Monday.
# pitfalls
- Regex cannot verify an address is deliverable. Always send a confirmation email for high-stakes forms.
- The full RFC 5322 grammar is hundreds of characters. The pattern above accepts ~99% of real addresses while rejecting obvious garbage. Use a fuller validator at the service boundary.
- Some real addresses are not matched (quoted local parts, IP-literal domains). Make peace with that.