Regex for URL Slug
A clean slug matcher for lowercase SEO paths. It permits internal hyphens while blocking uppercase letters, spaces, and repeated separators.
The expression
/^[a-z0-9]+(?:-[a-z0-9]+)*$/How to read it
Anchor: the match must start at the beginning of the string.
Anchor: the match must run to the end of the string.
(?: … ) groups without capturing, so it costs nothing to read back.
Nested quantifiers — the shape behind catastrophic backtracking.
Validate your own text, line by line
no inputThis expression is anchored, so it asks whether a whole string is valid. Each line above is tested on its own — it will not find a match inside a longer sentence.
Examples, checked in your browser
4/4 verifiednetwork-ports
Matches.
mime-types-guide
Matches.
BadSlug
No match — this is the kind of input the pattern rejects.
double--hyphen
No match — this is the kind of input the pattern rejects.
Use it in code
3 languagesconst regex = /^[a-z0-9]+(?:-[a-z0-9]+)*$/g;
regex.test(input);import re
pattern = re.compile(r"^[a-z0-9]+(?:-[a-z0-9]+)*$")
bool(pattern.search(input))re := regexp.MustCompile("^[a-z0-9]+(?:-[a-z0-9]+)*$")
matched := re.MatchString(input)Other validation patterns
6Valid Email Address
/^[^\s@]+@[^\s@]+\.[^\s@]+$/i
Strong Password
/^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[^A-Za-z\d]).{12,}$/
UUID v4
/^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i
IPv4 Address
/^(?:25[0-5]|2[0-4]\d|1?\d?\d)(?:\.(?:25[0-5]|2[0-4]\d|1?\d?\d)){3}$/
Hex Color
/^#?(?:[0-9a-fA-F]{3}|[0-9a-fA-F]{6})$/
Semantic Version
/^(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)(?:-[0-9A-Za-z-]+(?:\.[0-9A-Za-z-]+)*)?(?:\+[0-9A-Za-z-]+(?:\.[0-9A-Za-z-]+)*)?$/