Regex for Validating a URL Slug
Matches a lowercase, hyphen-separated URL-safe string.
Published October 12, 2026
Matches a lowercase, hyphen-separated URL-safe string.
Key points
- Rejects uppercase letters, spaces, and consecutive or leading/trailing hyphens
- Most slug-generation libraries lowercase and hyphenate a string first, then validate it against a pattern like this
Example
^[a-z0-9]+(?:-[a-z0-9]+)*$