T
ToolPrime

Regex for URL Slug

Validates URL-friendly slugs: lowercase, digits, hyphens only.

Pattern

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

Live Tester

Enter a string to test

Examples

my-blog-post
hello-world
page1
-starts-with
ends-with-
has--double

Pattern Breakdown

^[a-z0-9]+ — starts with lowercase alphanumeric

(-[a-z0-9]+)* — optional hyphen-separated groups

$ — no trailing hyphen

Code Snippets

Javascript

const regex = /^[a-z0-9]+(-[a-z0-9]+)*$/;
regex.test("my-blog-post"); // true

Python

import re
pattern = r"^[a-z0-9]+(-[a-z0-9]+)*$"
bool(re.match(pattern, "my-blog-post"))  # True

Php

$pattern = '/^[a-z0-9]+(-[a-z0-9]+)*$/';
preg_match($pattern, "my-blog-post"); // 1

Related Patterns

Frequently Asked Questions

What is the regex for URL Slug?
The regex pattern for URL Slug is /^[a-z0-9]+(-[a-z0-9]+)*$/. Validates URL-friendly slugs: lowercase, digits, hyphens only.
How do I use the URL Slug regex in JavaScript?
In JavaScript you create the pattern with a regex literal or the RegExp constructor and call .test() to check a string against it. A ready-to-copy example is shown in the Code Snippets section above, alongside Python and PHP versions.
What does the URL Slug pattern match?
It matches strings like my-blog-post; it rejects strings like -starts-with. See the Examples section above for the full list of matching and non-matching cases.

Related Tools