URL Slug
Validate URL slugs: lowercase letters and digits separated by single hyphens, no leading/trailing hyphens.
Try it in RegexPro →Available in
JavaScript / ECMAScript code
const re = new RegExp("^[a-z0-9]+(?:-[a-z0-9]+)*$", "");
const input = "my-blog-post";
const matches = [...input.matchAll(re)];
console.log(matches.map(m => m[0]));Uses `String.prototype.matchAll` for global iteration (Node 12+ / all modern browsers).
Python (re) code
import re
pattern = re.compile(r"^[a-z0-9]+(?:-[a-z0-9]+)*$")
input_text = "my-blog-post"
for m in pattern.finditer(input_text):
print(m.group(0))Stdlib `re` module — no third-party dependency. Works on Python 3.6+.
Go (RE2) code
package main
import (
"fmt"
"regexp"
)
func main() {
re := regexp.MustCompile(`^[a-z0-9]+(?:-[a-z0-9]+)*$`)
input := `my-blog-post`
for _, match := range re.FindAllString(input, -1) {
fmt.Println(match)
}
}Uses `regexp.MustCompile` (panics on bad patterns at startup) and `FindAllString` for all matches.
Pattern
^[a-z0-9]+(?:-[a-z0-9]+)*$Raw source: ^[a-z0-9]+(?:-[a-z0-9]+)*$
How it works
Examples
Input
my-blog-postMatches
my-blog-post
Input
hello-world-123Matches
hello-world-123
Input
-bad-slug-No match
—Common use cases
- •CMS URL slug validation
- •SEO-friendly URL generation
- •Blog post/product permalink checks
- •Route parameter sanitisation
Related patterns
Slug (Unicode Letters Allowed)
WebValidate URL slugs allowing Unicode letters (`café-rénové`), as supported by Next.js i18n routing.
HTML5 Color Input Value
WebValidate the value of an HTML5 `<input type="color">` — exactly 6 hex digits with leading hash.
URL Path Segment
WebMatch individual `/segment` parts of a URL path, capturing each one.
URL Query String
WebExtract the query string portion of a URL (everything between `?` and `#` or end-of-string).