Qaynaq LABS · TOOLS

REGEX · JAVASCRIPT

Regex explained

^[a-z0-9]+(?:-[a-z0-9]+)*\.?[a-z0-9-]*[a-z0-9]$

Matches strings that resemble domain names or slugs with optional hyphen-separated segments, an optional dot, and alphanumeric/hyphen characters, ending with an alphanumeric character.

Token-by-token breakdown

TokenMeaningExample
^ Anchor: asserts position at start of the string Start of 'example.com'
[a-z0-9]+ Character class: one or more lowercase letters or digits 'ex' in 'example'
(?: Start of non-capturing group Grouping for repetition
- Literal hyphen character Hyphen in 'sub-domain'
[a-z0-9]+ Character class: one or more lowercase letters or digits 'sub' in 'sub-domain'
) End of non-capturing group End of group
* Quantifier: zero or more repetitions of the preceding group Zero or more hyphen-separated segments
\.? Escaped literal dot followed by optional quantifier (zero or one) Dot in 'example.com' or absence in 'example'
[a-z0-9-]* Character class: zero or more lowercase letters, digits, or hyphens '-com' in 'example.com'
[a-z0-9] Character class: exactly one lowercase letter or digit 'm' in 'com'
$ Anchor: asserts position at end of the string End of 'example.com'

Matches

  • example
  • example123
  • sub-domain
  • sub-domain.example
  • a-b-c
  • 123-abc
  • a.b-c
  • a1-b2-c3

Does not match

  • Example
  • example..com
  • example-.com
  • -example.com
  • example.com.
  • example.com-
  • example@com
  • example com

Common pitfalls

  • Forgetting to escape the dot (\.) leading to incorrect matching of any character instead of a literal dot
  • Assuming the pattern matches all valid domain names (e.g., it does not support uppercase letters or underscores)
  • Overlooking the requirement for the string to end with an alphanumeric character, causing strings ending with hyphens to match incorrectly

Performance notes

The pattern is efficient due to the use of non-capturing groups and quantifiers. However, excessive use of quantifiers (e.g., nested *) could lead to catastrophic backtracking in pathological cases. Anchoring (^ and $) ensures the regex matches the entire string, which is optimal for validation tasks.

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 as \\.?

The dot is escaped to match a literal dot character. Without escaping, the dot would match any character except a newline, which is not the intended behavior for domain names or slugs.

Does this pattern support uppercase letters?

No, the pattern only matches lowercase letters due to the [a-z] character class. To support uppercase letters, the class would need to be expanded to [a-zA-Z].

Can this pattern match email addresses?

No, this pattern is designed for domain-like strings or slugs and does not account for the @ symbol or other email-specific syntax.