Regex to Match URL Slugs
Validate URL-friendly slug strings containing only lowercase letters, digits, and hyphens. No leading or trailing hyphens allowed. Free online regex tester.
Regular Expression
/^[a-z0-9]+(?:-[a-z0-9]+)*$/
Token Breakdown
| Token | Description |
|---|---|
| ^ | Anchors at the start of the string (or line in multiline mode) |
| [a-z0-9] | Character class — matches any one of: a-z0-9 |
| + | Matches the preceding element one or more times (greedy) |
| (?: | Start of non-capturing group |
| - | Matches the literal character '-' |
| [a-z0-9] | Character class — matches any one of: a-z0-9 |
| + | Matches the preceding element one or more times (greedy) |
| ) | End of group |
| * | Matches the preceding element zero or more times (greedy) |
| $ | Anchors at the end of the string (or line in multiline mode) |
Detailed Explanation
This regex validates URL slugs, which are the URL-friendly versions of page titles or resource names. Here is the token-by-token breakdown:
^ — Anchors the match at the start of the string, ensuring validation begins from the first character.
[a-z0-9]+ — Matches one or more lowercase letters or digits at the beginning of the slug. This ensures the slug starts with an alphanumeric character and prevents leading hyphens.
(?:-[a-z0-9]+)* — A non-capturing group that matches zero or more hyphen-separated word segments. Each segment consists of a single hyphen followed by one or more lowercase letters or digits. This structure prevents consecutive hyphens (like my--slug) and ensures each hyphen is surrounded by alphanumeric characters.
$ — Anchors the match at the end of the string, ensuring no trailing characters exist after the slug.
No flags are used because case sensitivity is intentional (slugs should be lowercase only) and we are validating a single complete string.
URL slugs are a critical component of SEO-friendly web addresses. Good slugs are lowercase, use hyphens as word separators, contain no special characters, and are human-readable. For example, a blog post titled "My First Post" would have the slug my-first-post.
This pattern is useful for content management systems, blog platforms, and any web application that generates clean URLs. It validates user input when creating custom slugs and ensures consistency across the site. Many frameworks use similar patterns in their routing systems to match URL path segments.
Example Test Strings
| Input | Expected |
|---|---|
| my-first-post | Match |
| hello-world-123 | Match |
| My-Post | No Match |
| -leading-hyphen | No Match |
| no spaces allowed | No Match |
| simple | Match |
Try It — Interactive Tester
26 charsFlags: noneMatches: 0Ctrl+Shift+C to copy regex
Related Regex Patterns
Regex to Match URLs
/https?:\/\/[\w.-]+(?:\.[a-zA-Z]{2,})(?:\/[\w./?#&=%-]*)*/gi
Regex to Match kebab-case Strings
/^[a-z][a-z0-9]*(?:-[a-z0-9]+)*$/
Regex to Extract Domain Names
/(?:https?:\/\/)?(?:www\.)?([a-zA-Z0-9-]+(?:\.[a-zA-Z]{2,})+)/gi
Regex to Match Programming Variable Names
/^[a-zA-Z_$][a-zA-Z0-9_$]*$/