Regex for URL Slug
Matches URL-friendly slugs: lowercase letters and digits separated by single hyphens. No leading/trailing hyphens.
Pattern
/^[a-z0-9]+(?:-[a-z0-9]+)*$/Pattern Breakdown
| Token | Explanation |
|---|---|
| ^ | Start of string |
| [a-z0-9]+ | One or more lowercase letters or digits |
| (?:-[a-z0-9]+)* | Optional repeating: hyphen followed by alphanumeric |
| $ | End of string |
Test Examples
| Input | Match? |
|---|---|
| my-blog-post | Match |
| page1 | Match |
| a-b-c | Match |
| -leading-dash | No match |
| UPPERCASE | No match |
| double--dash | No match |
Common Use Cases
- URL generation
- Content management systems
- Blog post URLs
- SEO-friendly paths
Variations
^[a-z0-9-]+$Simpler (allows leading/trailing/double hyphens)
Other Regex Patterns
Want to test this pattern with your own data?
Open the Regex Tester