Formatslug regex

URL slug (kebab-case) regex

Lowercase alphanumerics with single hyphens between words — the canonical URL slug shape.

Pattern
/^[a-z0-9]+(?:-[a-z0-9]+)*$/

What it matches

A URL slug is the human-readable identifier used in URLs: `hello-world`, `my-blog-post-2026`, `tooled-dev-launch`. The convention is lowercase, ASCII-only, hyphens between words, no leading/trailing/double hyphens, no spaces or special characters. This pattern enforces all of that.

Examples

Matches

  • hello-world

    Two words with single hyphen.

  • post-2026

    Letters and digits mixed.

  • x

    Single character is valid.

  • a-long-multi-word-slug

    Multiple hyphens are fine as long as no two are adjacent.

Does not match

  • Hello-World

    Uppercase characters.

  • hello--world

    Double hyphen.

  • -hello

    Leading hyphen.

  • hello-

    Trailing hyphen.

  • hello_world

    Underscore — slugs use hyphens.

  • hello world

    Space — slugs use hyphens.

Edge cases & gotchas

  • Doesn't accept Unicode — Cyrillic, Chinese, accented Latin all fail. If you need IDN-safe slugs, allow `\p{L}` (with the `u` flag) instead of `a-z`.
  • Allows leading digits (`2026-launch`) — fine for URLs but some systems treat all-digit slugs ambiguously with IDs.
  • Doesn't enforce a maximum length. Most CMSes cap at 50-100 characters; add `{1,100}` to the character class if you want.
  • Doesn't recognize stop words (`the`, `a`, `and`). Strip those when generating the slug, not when validating it.

In your language

// JavaScript
const re = /^[a-z0-9]+(?:-[a-z0-9]+)*$/;
const match = "input".match(re);

All 13 languages (including Bash, Perl, Kotlin, Swift) available in the full toolkit Export tab.

Notes for production

  • Generation: `slugify` (npm) or `python-slugify` handle accent stripping, lowercasing, and replacing whitespace with hyphens — much easier than rolling your own.
  • For SEO: keep slugs descriptive but short (3-5 words), include your target keyword, and never change them after publishing (use 301 redirects if you must).

Frequently asked

How do I generate a slug from a title?

Lowercase, strip accents, replace non-alphanumerics with hyphens, collapse consecutive hyphens to one, trim leading/trailing hyphens. Or use the `slugify` library — it's 30 lines of code that handles every edge case.

Why exclude underscores?

Google has historically treated hyphens as word separators and underscores as character separators (so `my_blog_post` is one 'word' to the crawler). Hyphens are the SEO-safe choice.

Can slugs contain digits?

Yes — this pattern accepts `2026-launch`, `version-1`, `top-10-tools`. Just avoid all-digit slugs since they're often confused with internal IDs.

Related patterns