Regex for URL Slug

All patterns
Validation26 chars

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 input
Try:

This 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 verified

network-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 languages
JavaScript
const regex = /^[a-z0-9]+(?:-[a-z0-9]+)*$/g;
regex.test(input);
Python
import re
pattern = re.compile(r"^[a-z0-9]+(?:-[a-z0-9]+)*$")
bool(pattern.search(input))
Go
re := regexp.MustCompile("^[a-z0-9]+(?:-[a-z0-9]+)*$")
matched := re.MatchString(input)

Other validation patterns

6
GroupValidation
Flagsnone
Examples4
SafetyOK