Regex for URL slug

Use /^[a-z0-9]+(?:-[a-z0-9]+)*$/ to check that a whole string is a valid for URL slug. Every example below is tested against the pattern, and you can open it in the Regex Tester with the samples preloaded.

Updated

/^[a-z0-9]+(?:-[a-z0-9]+)*$/
Open in Regex Tester
InputResult
regex-tester✓ Match
hello-world-2026✓ Match
a✓ Match
Hello-World✗ No match
double--dash✗ No match
-leading✗ No match
trailing-✗ No match
with space✗ No match

How the pattern works

PartMeaning
[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 false

Python

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.