Advertisement

Pattern library / Validation

URL Slug Regex (lowercase-with-hyphens)

A clean URL slug is lowercase letters, digits and single hyphens — no leading/trailing hyphen, no doubled hyphens, no spaces. This pattern enforces all of that in one line, which makes it useful for CMS validation, redirect hygiene checks and crawl audits.

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
ga4-regex-cheatsheetMatchcanonical slugTry →
post2Matchdigits allowedTry →
GA4-RegexNo matchuppercase rejectedTry →
-leadingNo matchcannot start with hyphenTry →
double--hyphenNo matchsingle hyphens onlyTry →
has spaceNo matchno spacesTry →

Where to use it

JavaScript (slug generator + check)

const slug = title.toLowerCase().trim().replace(/[^a-z0-9]+/g, '-').replace(/^-+|-+$/g, '');
const ok = /^[a-z0-9]+(-[a-z0-9]+)*$/.test(slug);

Python (redirect audit)

import re
SLUG = r'^[a-z0-9]+(-[a-z0-9]+)*$'
broken = [u for u in urls if not SLUG_RE.fullmatch(u.rsplit('/', 1)[-1])]

BigQuery / GA4 (RE2)

REGEXP_CONTAINS(page_path, r'^/[a-z0-9]+(-[a-z0-9]+)+/?$')

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

How do I generate a slug then validate it?

Generate: lowercase, replace any run of invalid characters with a single hyphen, trim leading/trailing hyphens. Validate the result with ^[a-z0-9]+(-[a-z0-9]+)*$.

Why does my slug with double hyphens fail?

By design — (-[a-z0-9]+)* requires each hyphen to be followed by core characters, so a--b has an empty segment and fails.

Does this work for full URLs?

No — it validates a single slug segment. For paths, anchor a segment pattern like ^/[a-z0-9]+(-[a-z0-9]+)*/?$ instead.

URL Regex — Match http/https Links (Practical) · Email Validation Regex — Practical Pattern to Test · Remove Query Parameters with Regex (Replace Example)

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.