tools / regex library / url slug
regex pattern · ready to copy

Regex for matching URL slugs (lowercase, hyphenated)

Match SEO-friendly URL slugs: lowercase letters, digits, hyphens. No leading/trailing hyphen.

intermediate javascript / pcre / python4 use cases

The pattern

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

Test cases

InputResult
my-awesome-postmatches
tools-2026matches
regex-101matches
My-Post (uppercase)rejects
-leading-dashrejects
trailing-dash-rejects
double--hyphenrejects

Edge cases & caveats

Excludes underscores and dots. For Git branch slugs, allow `/`. For diacritic transliteration before regex (e.g., `café` → `cafe`), use a slugify lib first.

Common use cases

Try variations against your data
regexlab is a free in-browser tester with side-by-side match highlighting, group inspector, and named-capture export to JS/Python/PCRE.
Open regexlab  

Related

credit card number · us zip code · sql injection keywords · phone number · ↗ transliterate non-Latin titles