Pattern library / Strings & Text
Strict Regex for URL Slugs (Lowercase Only)
Stricter than the general slug regex: enforces lowercase only and forbids leading/trailing hyphens or consecutive hyphens.
How the pattern works
[a-z0-9]+— starts with lowercase/digit.(-[a-z0-9]+)*— hyphen followed by lowercase/digit, repeated.- Forbids leading, trailing, and consecutive hyphens by construction.
- Add
_to the class if snake_case should also pass.
Test it: match & no-match examples
Every row is loaded straight into the tester — click Try to see the match live:
| Input | Result | Why | |
|---|---|---|---|
hello-world | Match | valid slug | Try → |
abc123 | Match | no hyphens ok | Try → |
Hello-World | No match | uppercase rejected | Try → |
-hello | No match | leading hyphen rejected | Try → |
hello--world | No match | double hyphen rejected | Try → |
hello_ | No match | underscore rejected | Try → |
Where to use it
JavaScript
const re = /^[a-z0-9]+(-[a-z0-9]+)*$/;
re.test('hello-world'); // true
Python
import re re.match(r'^[a-z0-9]+(-[a-z0-9]+)*$', 'hello-world')
Common mistakes
Test this pattern on your own data
Paste it into the free tester with your real strings — nothing leaves your browser.
FAQ
Related patterns
For GA4-specific patterns, see the GA4 regex cheat sheet, the page path regex guide, the event filter regex guide, the GA4 regex match types, the custom channel grouping guide and RE2 vs PCRE. Browse the full regex pattern library.