Qaynaq LABS · TOOLS

REGEX · JAVASCRIPT

Regex explained

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

Matches an email address with a domain that has two top-level domains (e.g., 'example.co.uk'). The pattern enforces a username, followed by '@', then a domain with at least one dot-separated segment, and two TLDs each with at least two characters.

Token-by-token breakdown

TokenMeaningExample
^ Anchor: asserts position at start of the string Start of 'user@example.co.uk'
[a-zA-Z0-9._%+-]+ Character class matching one or more alphanumeric, dot, underscore, percent, plus, or hyphen characters (username part) user, user.name, user+123
@ Literal '@' character @ in 'user@example'
[a-zA-Z0-9.-]+ Character class matching one or more alphanumeric, dot, or hyphen characters (domain name part) example, sub.example
\. Escaped literal dot (matches a single '.') .
[a-zA-Z]{2,} Character class matching two or more alphabetic characters (first TLD) co, uk
\. Escaped literal dot (matches a single '.') .
[a-zA-Z]{2,} Character class matching two or more alphabetic characters (second TLD) uk, com
$ Anchor: asserts position at end of the string End of 'user@example.co.uk'

Matches

  • user@example.co.uk
  • name+tag@sub.domain.co.jp
  • a@b.cd.ef
  • 123.456@789.abc.def

Does not match

  • user@example.com
  • user@.com.uk
  • user@example..com.uk
  • user@example.c
  • user@example.co.uk.
  • @example.co.uk
  • user@.co.uk

Common pitfalls

  • The pattern requires exactly two TLDs, so 'user@example.com' fails (only one TLD).
  • The username part allows special characters like '%' and '+', which may not be valid in all email systems.
  • The pattern does not enforce length limits, which could lead to overly long inputs.
  • The domain part allows consecutive dots (e.g., 'user@sub..domain.co.uk'), which is invalid in practice.
  • The pattern does not account for internationalized domain names (IDNs) or non-ASCII characters.

Performance notes

The pattern is generally efficient due to simple character classes and quantifiers. However, the use of multiple quantifiers (e.g., '+', '{2,}') could lead to catastrophic backtracking if the input is maliciously crafted (e.g., very long strings of allowed characters). Anchoring (^ and $) ensures the entire string is validated, which is good practice.

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 pattern require two TLDs (e.g., 'co.uk')?

The pattern is designed to match email addresses with a domain that has two top-level domains, such as country-code TLDs (e.g., '.co.uk', '.com.br'). This is not a universal requirement for email addresses, but it fits specific use cases.

Does this pattern validate real email addresses?

No. This pattern only checks the syntactic structure of the email address. It does not verify whether the domain exists, the username is valid, or the email is deliverable. For validation, additional checks (e.g., DNS lookup) are required.

Can this pattern match email addresses with subdomains (e.g., 'user@mail.example.co.uk')?

Yes. The domain part ([a-zA-Z0-9.-]+) allows subdomains (e.g., 'mail.example'), so 'user@mail.example.co.uk' would match.

Why is the username part so permissive (e.g., allowing '%', '+', '-')?

The username part is designed to match common email address formats, which often include these characters. However, some systems may restrict these characters, so additional validation may be needed depending on the use case.