URL SlugRegex — Pattern, Examples & Code
^[a-z0-9]+(?:-[a-z0-9]+)*$What it matches / doesn't match
| Matches ✅ | Does not match ❌ |
|---|---|
●hello-world | ●-leading-hyphen |
●my-blog-post-2024 | ●trailing-hyphen- |
●api-v2 | ●double--hyphen |
How it works
Matches URL-safe slugs consisting of lowercase letters and digits, with single hyphens as word separators. Leading hyphens, trailing hyphens, and consecutive hyphens are all rejected. This pattern does not allow uppercase letters or underscores — normalize the input string first if needed.
Code Usage
JavaScript
const regex = /^[a-z0-9]+(?:-[a-z0-9]+)*$/;
regex.test("hello-world"); // truePython
import re
pattern = re.compile(r'^[a-z0-9]+(?:-[a-z0-9]+)*$')
bool(pattern.match("hello-world")) # TrueGo
import "regexp"
r := regexp.MustCompile(`^[a-z0-9]+(?:-[a-z0-9]+)*$`)
r.MatchString("hello-world") // trueNeed a custom regex for a different use case?
Generate a custom regex with AI →FAQ
What does the URL Slug regex pattern match?
Matches URL-safe slugs consisting of lowercase letters and digits, with single hyphens as word separators. Leading hyphens, trailing hyphens, and consecutive hyphens are all rejected. This pattern does not allow uppercase letters or underscores — normalize the input string first if needed.
How do I use the URL Slug regex in JavaScript?
const regex = /^[a-z0-9]+(?:-[a-z0-9]+)*$/; regex.test("hello-world"); // true
Can I use the URL Slug regex in Python?
Yes. import re pattern = re.compile(r'^[a-z0-9]+(?:-[a-z0-9]+)*$') bool(pattern.match("hello-world")) # True
