Qaynaq LABS · TOOLS

REGEX · JAVASCRIPT

Regex explained

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

Matches a string that represents a domain name or similar identifier, starting with alphanumeric characters, hyphens, or dots, followed by a dot and a top-level domain of at least 2 letters.

Token-by-token breakdown

TokenMeaningExample
^ Anchor: asserts position at start of the string Start of 'example.com'
[a-zA-Z0-9\-\.]+ Character class matching one or more alphanumeric characters, hyphens, or dots Matches 'example', 'sub-domain', 'test.'
+ Quantifier: one or more of the preceding token Required for the first part of the domain
\. Escaped literal dot character (matches a single '.') The dot before the TLD in 'example.com'
[a-zA-Z]{2,} Character class matching 2 or more alphabetic characters Matches 'com', 'org', 'io'
$ Anchor: asserts position at end of the string End of 'example.com'

Matches

  • example.com
  • sub.domain.co
  • test-domain.org
  • a.b.c.d
  • 123.456.xyz
  • my-site.net

Does not match

  • example..com
  • example.c
  • example.com.
  • .example.com
  • example@com
  • example com
  • example.c0m
  • example.com-

Common pitfalls

  • Fails to match domains with underscores (e.g., 'example_domain.com')
  • Does not validate internationalized domain names (IDNs)
  • Allows consecutive dots (e.g., 'ex..ample.com') which are invalid in real domains
  • Does not enforce TLD length limits (e.g., 'example.technology' is valid but long)
  • Hyphens at start/end of labels are allowed but may be invalid in practice (e.g., '-example.com')
  • Escaping the dot is correct here, but forgetting to escape it would make it a wildcard

Performance notes

The pattern is efficient with no catastrophic backtracking risk. The anchors (^ and $) ensure the entire string is matched, preventing partial matches. The character class [a-zA-Z0-9\-\.] is optimized for ASCII and does not require alternation.

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 hyphen escaped in the character class?

The hyphen is escaped (\-) to avoid being interpreted as a range operator within the character class. Without escaping, 'a-z' would denote a range, but here we want a literal hyphen.

Does this pattern validate real domain names?

No, this pattern is a simplified regex for demonstration. Real domain validation requires handling IDNs, subdomains, and other RFC-compliant rules.

Why is the dot escaped in the pattern?

The dot is escaped (\.) to match a literal dot character. Without escaping, '.' would match any character except a newline.

Can this pattern match email addresses?

No, this pattern is designed for domain-like strings (e.g., 'example.com'), not full email addresses (e.g., 'user@example.com').