Qaynaq LABS · TOOLS

REGEX · JAVASCRIPT

Regex explained

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

Matches strings that start with alphanumeric characters, followed by optional dot-separated alphanumeric segments, then a forward slash, and ending with alphanumeric characters or hyphens.

Token-by-token breakdown

TokenMeaningExample
^ Anchor: asserts the position at the start of the string Start of 'abc123/def'
[a-z0-9] Character class: matches any lowercase letter or digit 'a', '1'
+ Quantifier: matches one or more of the preceding token 'abc123'
(?: Non-capturing group start: groups tokens without capturing Start of '(?:\.[a-z0-9]+)*'
\. Escaped character: matches a literal dot '.' in 'abc.123'
[a-z0-9] Character class: matches any lowercase letter or digit 'a', '1'
)+ Quantifier: matches zero or more occurrences of the preceding group '.123' in 'abc.123'
\/ Escaped character: matches a literal forward slash '/' in 'abc/def'
[a-z0-9-] Character class: matches any lowercase letter, digit, or hyphen 'a', '1', '-'
+ Quantifier: matches one or more of the preceding token 'def-456'
$ Anchor: asserts the position at the end of the string End of 'abc123/def'

Matches

  • abc/def
  • a1b2c3/456
  • test123/foo-bar
  • x/y
  • 123/456

Does not match

  • abc.def/ghi
  • abc/def/ghi
  • abc def/ghi
  • ABC/def
  • abc//def
  • /abc/def
  • abc/def-
  • abc/def.

Common pitfalls

  • Forgetting to escape the forward slash (\/) can lead to incorrect matches.
  • The pattern does not allow uppercase letters, which may be unexpected in some contexts.
  • The pattern does not allow underscores or other special characters in the segments.
  • The pattern does not allow empty segments (e.g., 'a..b/c' or 'a//b').

Performance notes

The pattern is efficient as it uses non-capturing groups and avoids nested quantifiers. No catastrophic backtracking risk. Ensure input strings are not excessively long to maintain performance.

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 (...)?

Non-capturing groups (?:...) are 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 pattern explicitly uses [a-z0-9], which only matches lowercase letters and digits. To include uppercase, use [a-zA-Z0-9].

Does this pattern allow hyphens in the first segment (before the slash)?

No, the first segment (before the slash) only allows lowercase letters and digits. Hyphens are only allowed in the segment after the slash.