Qaynaq LABS · TOOLS

REGEX · JAVASCRIPT

Regex explained

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

Matches strings composed of lowercase letters and digits, with optional hyphen-separated segments. The entire string must start and end with alphanumeric characters.

Token-by-token breakdown

TokenMeaningExample
^ Anchor: asserts position at start of the string Start of 'abc-123'
[a-z0-9] Character class: matches any lowercase letter (a-z) or digit (0-9) a, b, 1, 9
+ Quantifier: matches one or more of the preceding token (greedy) abc, 123, a1b2
(?: Non-capturing group start: groups tokens without capturing Start of '-(a-z0-9)+' segment
- Literal hyphen: matches a hyphen character -
[a-z0-9] Character class: matches any lowercase letter (a-z) or digit (0-9) a, b, 1, 9
+ Quantifier: matches one or more of the preceding token (greedy) abc, 123, a1b2
) Non-capturing group end End of '-(a-z0-9)+' segment
* Quantifier: matches zero or more of the preceding non-capturing group (greedy) No hyphen, '-abc', '-123-456'
$ Anchor: asserts position at end of the string End of 'abc-123'

Matches

  • abc
  • 123
  • a1b2
  • abc-123
  • a-b-c-1-2-3
  • a1-b2-c3
  • 1
  • a

Does not match

  • ABC
  • abc_123
  • abc--123
  • -abc
  • abc-
  • abc 123
  • a b c
  • a..b..c

Common pitfalls

  • Case sensitivity: uppercase letters are not matched by [a-z]
  • Hyphen placement: strings starting or ending with hyphens are invalid
  • Empty strings: not matched due to '+' quantifier
  • Underscores or other special characters are not allowed

Performance notes

The pattern is efficient as it avoids catastrophic backtracking. The non-capturing group (?:...) is used to improve performance by avoiding unnecessary capture. The anchors ^ and $ ensure the entire string is matched, 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 use (?:...) instead of (...)?

The non-capturing group (?:...) is used to group tokens without creating a capture group, which improves performance by avoiding unnecessary memory allocation for captured groups.

Can this pattern match uppercase letters?

No, the character class [a-z0-9] only matches lowercase letters and digits. To include uppercase, use [a-zA-Z0-9].

Does this pattern allow empty strings?

No, the '+' quantifier requires at least one character in the initial segment, and the '*' quantifier allows zero or more subsequent segments, but the entire string must contain at least one character.