Qaynaq LABS · TOOLS

REGEX · JAVASCRIPT

Regex explained

^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$

Validates a basic email address format with a local part, @ symbol, domain name, and top-level domain of at least 2 letters.

Token-by-token breakdown

TokenMeaningExample
^ Anchor: asserts position at start of the string Start of 'user@example.com'
[a-zA-Z0-9._%+-] Character class: matches any single alphanumeric character, dot, underscore, percent, plus, or hyphen 'u', 's', 'e', '.', '_', '%', '+', '-'
+ Quantifier: matches one or more of the preceding token (greedy) Matches one or more characters in 'user123'
@ Literal character: matches the @ symbol literally '@'
[a-zA-Z0-9.-] Character class: matches any single alphanumeric character, dot, or hyphen 'e', 'x', 'a', '.', '-'
+ Quantifier: matches one or more of the preceding token (greedy) Matches one or more characters in 'example'
\. Escaped literal: matches a literal dot (.) character '.'
[a-zA-Z] Character class: matches any single alphabetic character (case-insensitive) 'c', 'o', 'm'
{2,} Quantifier: matches 2 or more of the preceding token (greedy) Matches 'com' in 'example.com'
$ Anchor: asserts position at end of the string End of 'user@example.com'

Matches

  • user@example.com
  • john.doe123@sub.domain.co
  • a@b.c
  • test_email+filter@domain.org
  • 123.456@789.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

  • Fails to validate internationalized email addresses (non-ASCII characters)
  • Does not enforce length limits on the local part or domain
  • Allows consecutive dots in the local part (e.g., 'user..name@example.com')
  • Does not validate domain labels (e.g., 'example..com')
  • Does not handle quoted strings in the local part (e.g., '"user name"@example.com')

Performance notes

The pattern is efficient for typical email validation as it uses simple character classes and quantifiers. No catastrophic backtracking risk due to the absence of nested quantifiers or alternations. Anchors ensure 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 is the dot escaped in '\\.'?

The dot is escaped to match a literal dot character instead of its special regex meaning (which is any character except newline). In JavaScript regex, escaping is done with a backslash.

Does this pattern validate all valid email addresses?

No. This pattern is a simplified validation and does not cover all edge cases defined in RFC 5322. For full validation, a more complex regex or dedicated library is recommended.

Can this pattern match email addresses with subdomains?

Yes, the pattern allows for subdomains in the domain part (e.g., 'user@sub.example.com') as the domain part is matched by '[a-zA-Z0-9.-]+' followed by a dot and TLD.