Regular Expressions 101

Email validation for modern applications

Open Workspace
  • JavaScript
  • Unit Tests
  • Recommended
3

Regular Expression

/
^(?=.{6,254}$)(?=[^@]{1,64}@)(?<local_part>(?=['._-]*[A-Za-z0-9])(?<username>[A-Za-z0-9'_-]+(?:\.[A-Za-z0-9'_-]+)*)(?:\+(?<plus_alias>(?:\.?[A-Za-z0-9+_-]+(?:\.[A-Za-z0-9+_-]+)*)?))?)@(?<domain>(?:[A-Za-z0-9](?:[A-Za-z0-9-]{0,61}[A-Za-z0-9])?\.)+(?:[A-Za-z]{2,63}|[Xx][Nn]--[A-Za-z0-9-]{0,58}[A-Za-z0-9]))$
/
gm

Description

Practical email validation for modern applications. Balances common address formats with strict length, dot and domain rules. Supports plus addressing, long TLDs and ASCII internationalized domains that simpler patterns may reject. Includes named captures and extensive tests. A practical fit for most SaaS signups, contact forms and contact management. Rules neither too loose nor too strict.

Supported cases

  • Ordinary syntax at Gmail, Outlook/Hotmail, Yahoo, iCloud and work domains.
  • Uppercase and lowercase ASCII letters, numbers, dots, underscores, hyphens and apostrophes in usernames.
  • Plus addressing supports empty tags and additional + signs. Trailing dots and consecutive dots are rejected.
  • Usernames may start or end with _, - or ', but must contain a letter or digit.
  • Subdomains, numeric domain labels and internal hyphens.
  • Alphabetic TLDs of 2–63 characters and ASCII xn-- domain forms for internationalized names.
  • Checks character lengths:
  • Total: 6–254.
  • Local part: 1–64 (including plus addressing).
  • Domain labels: 1–63.
  • Named captures: local_part, username, plus_alias, domain.

The regex checks for the xn-- structure, not full IDNA validity .

Valid examples

Narrower patterns may reject some of these valid email address formats.

Accepted exampleWhat it demonstrates
user.name@example.comCommon dotted username
User.Name@Example.ComMixed case
o'connor@example.comApostrophe in the username
_support-@example.comPermitted username edges
user+news@example.comPlus addressing
user+@example.comEmpty plus tag
user+news+work@example.comAdditional plus signs
user@example.technologyLonger TLD
user@xn--bcher-kva.deInternationalized domain in ASCII

Invalid examples

Loose patterns may accept some of these malformed email addresses or input formats.

Rejected exampleReason
.user@example.comLeading local-part dot
user.@example.comTrailing local-part dot
user..name@example.comConsecutive local-part dots
user@example..comEmpty domain label
user@_example.comUnderscore in the domain
user@-example.comLeading domain-label hyphen
user@name@example.comMultiple @ signs
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa@example.comLocal part exceeds 64 characters
user name@example.comSpace in local part
User Name <user@example.com>Display-name wrapper, not a bare address
user@example.com,other@example.comAddress list, not one address

Tabs, CR/LF, control characters and surrounding text are also rejected.

Intentional limits

These policy exclusions include standards-valid forms; they are not inherently unsafe. See RFC 5321 and SMTPUTF8 .

Excluded caseExample
IP-literal domainuser@[127.0.0.1]
Single-label domainuser@localhost
Quoted local part"user"@example.com
Raw Unicode address名前@example.com
Extra local-part symbols: !, %, /, =user%name@example.com
Punctuation-only username___@example.com
Apostrophe inside a plus taguser+new's@example.com
Username dot immediately before +user.+news@example.com

Tests and security

Verified against 560 test cases, covering valid and invalid forms, length boundaries, aliases, domains, case and control characters.

Length guards and dot-separated segments limit ambiguity associated with catastrophic backtracking . Adversarial tests found no catastrophic slowdown in the tested engines.

Rejecting control characters reduces log-injection risks. Still use server-side validation, safe mail APIs, parameterized queries, output encoding and email verification. Regex cannot prove domain existence, mailbox ownership or deliverability, or prevent injection alone. See OWASP guidance .

Speed

Measured across all the test suite: median of seven batch averages, precompiled regex. Speed depends on hardware, engine and input.

EngineDuration per validation
Node.js 24.19~0.13 µs
Ruby 3.2.2~0.92 µs

About regex101.com performance: Change the Unit Tests tab to "Show only failed tests" to improve the tests speed.

Usage

Use the JavaScript flavor without flags.

To enforce whole-string matching for Ruby/PCRE2 , replace the opening ^ with \A and both $ anchors with \z (one inside the length lookahead and one at the end). Other flavors may need syntax changes.

Submitted by Tiago Cussiol