Web

URL Slug

Validate URL slugs: lowercase letters and digits separated by single hyphens, no leading/trailing hyphens.

Try it in RegexPro →

Available in

JavaScript / ECMAScript code

jsJavaScript
const re = new RegExp("^[a-z0-9]+(?:-[a-z0-9]+)*$", "");
const input = "my-blog-post";
const match = input.match(re);
console.log(match ? match[0] : null);

Uses `String.prototype.match`, which returns the first match or null.

Pattern

regexengine-agnostic
^[a-z0-9]+(?:-[a-z0-9]+)*$

Raw source: ^[a-z0-9]+(?:-[a-z0-9]+)*$

How it works

Anchored with ^ and $. Requires at least one alphanumeric character. Hyphens can only appear between alphanumeric groups — not at start or end.

Examples

Input

my-blog-post

Matches

  • my-blog-post

Input

hello-world-123

Matches

  • hello-world-123

Input

-bad-slug-

No match

—

Common use cases

  • •CMS URL slug validation
  • •SEO-friendly URL generation
  • •Blog post/product permalink checks
  • •Route parameter sanitisation

Related patterns

Related concepts