Advertisement

Pattern library / Strings & Text

Strict Regex for URL Slugs (Lowercase Only)

Stricter than the general slug regex: enforces lowercase only and forbids leading/trailing hyphens or consecutive hyphens.

The pattern
^[a-z0-9]+(-[a-z0-9]+)*$
Load this pattern into the tester →

How the pattern works

Test it: match & no-match examples

Every row is loaded straight into the tester — click Try to see the match live:

InputResultWhy
hello-worldMatchvalid slugTry →
abc123Matchno hyphens okTry →
Hello-WorldNo matchuppercase rejectedTry →
-helloNo matchleading hyphen rejectedTry →
hello--worldNo matchdouble hyphen rejectedTry →
hello_No matchunderscore rejectedTry →

Where to use it

JavaScript

const re = /^[a-z0-9]+(-[a-z0-9]+)*$/;
re.test('hello-world');  // true

Python

import re
re.match(r'^[a-z0-9]+(-[a-z0-9]+)*$', 'hello-world')

Common mistakes

Test this pattern on your own data
Paste it into the free tester with your real strings — nothing leaves your browser.
Open the regex tester →

FAQ

For GA4-specific patterns, see the GA4 regex cheat sheet, the page path regex guide, the event filter regex guide, the GA4 regex match types, the custom channel grouping guide and RE2 vs PCRE. Browse the full regex pattern library.