Qaynaq LABS · TOOLS

REGEX · JAVASCRIPT

Regex explained

^[\w\.\-]+@[\w\.\-]+\.[a-zA-Z]{2,}$

This regex matches a basic email address format with a local part, '@' symbol, domain name, and a top-level domain of at least 2 letters. It does not enforce strict RFC 5322 compliance.

Token-by-token breakdown

TokenMeaningExample
^ Anchor: asserts the position at the start of the string Start of 'user@example.com'
[ Start of a character class N/A
\w Matches any word character (letters, digits, underscore, or hyphen) a-z, A-Z, 0-9, _
\. Matches a literal dot character .
\- Matches a literal hyphen character -
] End of the character class N/A
+ Quantifier: matches one or more of the preceding token (character class) user, user.name, user-name
@ Matches a literal '@' symbol @
[ Start of a character class N/A
\w Matches any word character (letters, digits, underscore, or hyphen) a-z, A-Z, 0-9, _
\. Matches a literal dot character .
\- Matches a literal hyphen character -
] End of the character class N/A
+ Quantifier: matches one or more of the preceding token (character class) example, sub.example, sub-example
\. Matches a literal dot character .
[ Start of a character class N/A
a-zA-Z Matches any uppercase or lowercase letter a, B, z
] End of the character class N/A
{2,} Quantifier: matches 2 or more of the preceding token (letter class) com, io, org
$ Anchor: asserts the position at the end of the string End of 'user@example.com'

Matches

  • user@example.com
  • user.name@sub.domain.co
  • user-name@domain.io
  • a@b.cd
  • 123@456.xyz

Does not match

  • user@.com
  • @example.com
  • user@example..com
  • user@example.c
  • user@example.com.
  • user@-example.com
  • user@example-.com

Common pitfalls

  • Does not allow quoted strings in the local part (e.g., "user name"@example.com)
  • Does not enforce a maximum length for the local or domain parts
  • Does not validate internationalized domain names (IDNs)
  • Allows consecutive dots in the domain part (e.g., user@ex..ample.com)
  • Does not restrict the top-level domain to common values (e.g., .com, .org)
  • Allows hyphens at the start or end of domain labels (e.g., user@-example.com)

Performance notes

The pattern is linear in complexity and does not pose a risk of catastrophic backtracking. However, excessively long strings may still impact performance due to the unbounded quantifiers (+). Anchoring at both ends ensures the entire string is validated, preventing partial matches.

About regular expressions

Regular expressions are tiny programs that match patterns in text. They're powerful and notoriously cryptic — most engineers can write them but few can read them confidently. Qaynaq Labs' regex explainer breaks any pattern into its constituent tokens, shows what each one does, and runs through both matching and non-matching examples so you can verify the logic.

Different regex flavors (JavaScript, PCRE, Python, Go) have subtle differences in syntax and supported features. Pick your flavor in the input — the explainer accounts for it.

Frequently asked

Why does this regex allow underscores in the email address?

The \w token includes underscores, which are technically allowed in the local part of an email address per RFC 5322, though some systems may restrict them.

Does this regex validate all valid email addresses?

No. This regex is a simplified pattern and does not cover all edge cases defined in RFC 5322, such as quoted strings, comments, or special characters in the local part.

Can this regex match internationalized email addresses (e.g., with non-ASCII characters)?

No. The regex only matches ASCII characters. Internationalized email addresses require Unicode support (e.g., using \p{L} in Unicode-aware regex flavors).

Why is the top-level domain restricted to letters only?

The [a-zA-Z]{2,} token ensures the top-level domain consists only of letters, which is common but not universally required (e.g., some country-code TLDs may include digits).