How the pattern works
| Part | Meaning |
|---|---|
[a-z0-9]+ | One or more lower-case letters or digits |
(?:-[a-z0-9]+)* | Any number of further words, each after a single hyphen |
^…$ | The whole string — so no leading, trailing or double hyphens |
Code examples
JavaScript
const re = /^[a-z0-9]+(?:-[a-z0-9]+)*$/;
re.test(value); // true or falsePython
import re
bool(re.fullmatch(r'[a-z0-9]+(?:-[a-z0-9]+)*', value))PHP
preg_match('/^[a-z0-9]+(?:-[a-z0-9]+)*$/', $value) === 1;
Notes and limitations
Turn titles into slugs with the Slug Generator.
Regex TesterType a pattern and watch matches highlight live in your test text.
Open the tool Frequently asked questions
What is the regex for URL slug?
/^[a-z0-9]+(?:-[a-z0-9]+)*$/ — it matches the whole string. See the table above for what each part does.
Which examples match?
Of the examples tested, these match: regex-tester, hello-world-2026, a. The others don’t.
How do I test this regex?
Click “Open in Regex Tester” — the pattern, flags and sample text load automatically, and matches highlight as you edit.