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
| Input | Result |
|---|---|
| my-awesome-post | ✓matches |
| tools-2026 | ✓matches |
| regex-101 | ✓matches |
| My-Post (uppercase) | ✗rejects |
| -leading-dash | ✗rejects |
| trailing-dash- | ✗rejects |
| double--hyphen | ✗rejects |
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
- CMS post slug validation
- vanity URL scrubbing
- SEO audit
- i18n routing key validation
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