JavaScript-compatible regex reference

URL Slug Regex Pattern & Tester

Validate lowercase, hyphen-separated URL slugs such as regex-pattern-library or product-2026. The pattern requires alphanumeric segments separated by single hyphens, so leading, trailing, and repeated hyphens are rejected.

Pattern

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

Flags: none

Live test vectors

Test the URL slug regex

Enter one value per line. Each line is tested against the whole-string pattern using the browser JavaScript RegExp engine.

PASSregex-pattern-library
PASSproduct-2026
PASSjson-tools
PASSa
FAILRegex-Pattern
FAIL-leading-hyphen
FAILdouble--hyphen
FAILtrailing-hyphen-

Quick answer

For a simple lowercase ASCII slug policy, require one or more alphanumeric segments separated by single hyphens. Generate or normalize Unicode and punctuation before validation rather than silently accepting arbitrary characters.

Common use cases

  • CMS route validation
  • Static-page slug checks
  • API resource identifiers

Pitfalls and limitations

  • !This pattern intentionally rejects uppercase letters and underscores.
  • !Internationalized slugs need a Unicode-aware policy rather than an ASCII-only pattern.
  • !A validator does not perform slugification; normalize spaces, accents, punctuation, and case before validation when generating slugs from titles.

Known-valid examples

regex-pattern-libraryproduct-2026json-toolsa

Known-invalid examples

Regex-Pattern-leading-hyphendouble--hyphentrailing-hyphen-

Continue testing and debugging

Frequently asked questions

Why does the pattern reject underscores?

The policy represented here uses hyphens as the only separator. If your application permits underscores, change the separator rule deliberately rather than broadening the character class accidentally.

Does this regex create a slug from a title?

No. It validates an already-normalized slug. Slug generation usually lowercases text, transliterates or preserves Unicode according to policy, removes punctuation, and collapses whitespace into separators.

Can slugs contain Unicode characters?

They can, depending on your routing and product requirements. This page targets a simple ASCII lowercase slug convention; Unicode-aware slugs require a different character policy and regex.