Back to the tool

Regex Pattern to Validate a URL Slug: the One Liner, and Where It Stops

Regex pattern to validate a URL slug: ^[a-z0-9]+(?:-[a-z0-9]+)*$ is the one to start with. It allows lowercase letters, digits, and single hyphens between words, and it rejects leading hyphens, trailing hyphens, double hyphens, and anything uppercase. It is eleven tokens of genuinely good engineering, and it is also not enough on its own, because the slugs it accepts still need a uniqueness check, a length cap, and a slug generator that does not produce them by accident.

Let me take the pattern apart, because each piece earns its place. ^ anchors the match to the start of the string, and $ anchors it to the end, so the whole input must be a slug rather than containing one somewhere inside. [a-z0-9]+ requires one or more lowercase letters or digits to start, which rules out a leading hyphen and an empty string in one move. (?:-[a-z0-9]+)* is the repeating tail: a hyphen followed by one or more letters or digits, repeated zero or more times. Because every hyphen must be followed by at least one word character, double hyphens fail, and because the group must end on a word character, a trailing hyphen fails. The non capturing group, (?:...), keeps the match clean without creating a capture you will never use.

What it accepts, and what it rejects

Run it against real inputs and the behavior is exactly what a slug validator should do. "hello-world" passes. "123-abc" passes. "this-is-a-slug-url" passes. "-hello" fails on the leading hyphen. "hello-" fails on the trailing one. "hello--world" fails on the double hyphen. "Hello-World" fails on the uppercase, which is a deliberate choice, not a limitation: mixed case slugs mean one page living at two addresses, and case sensitive servers will serve both. Normalize to lowercase before validating, and reject anything that arrives uppercase rather than silently fixing it, because a silent fix hides the input problem that produced it.

The pattern also fails one class of input people expect it to pass, and that is worth naming: non English slugs. "creme-brulee" passes, but "crème-brûlée" does not, because è and û are outside a-z. The standard fix is transliteration before validation, turning "crème brûlée" into "creme-brulee" with a lookup table or a library, then running the pattern. Regex alone cannot transliterate. Anyone who tells you a single pattern handles Unicode slugs cleanly is selling you a pattern with a character class so broad it accepts punctuation too.

Regex pattern to validate a URL slug in real code

In practice, validation is the second step, not the first. The first step is generation: take a title, lowercase it, replace every run of non alphanumeric characters with a single hyphen, and trim hyphens from both ends. In JavaScript that is title.toLowerCase().replace(/[^a-z0-9]+/g, '-').replace(/^-+|-+$/g, ''). Only then run the validation pattern on the result. The generator produces clean slugs from messy titles, and the validator catches the cases the generator mishandles, like a title that is entirely punctuation and collapses to an empty string.

Then add the three checks regex cannot do. A length cap, because a 200 character slug is valid by pattern and unreadable in search results; aim for 60 characters or fewer. A uniqueness check against your database or file list, because two posts titled the same thing generate the same slug, and the second one needs a suffix like "-2" before it ships. And a reserved word check, because a slug like "admin" or "login" can collide with a route your app already owns. The pattern guards the shape. These three guard the system around it.

My rule of thumb: if your slug validation is one line of regex and nothing else, you have validated the easy half. The failures that reach production are never "hello--world" slipping through. They are duplicates, collisions, and 180 character slugs generated from long titles, and none of those are regex problems. Build and test the pattern here with templates for slugs and the other common formats, and pair it with a uniqueness check before anything goes live.

Frequently asked questions

What is the regex to validate a URL slug?

^[a-z0-9]+(?:-[a-z0-9]+)*$ is the standard pattern. It allows lowercase letters, digits, and single hyphens between words, and rejects leading, trailing, and double hyphens, uppercase letters, and empty strings.

Why does the slug pattern reject uppercase letters?

Because mixed case slugs let one page live at two addresses, and case sensitive servers serve both. Normalize input to lowercase before validating, and keep slugs lowercase everywhere.

Can a regex validate a slug with accented characters?

Not cleanly. Characters like è and û fall outside a-z, so the standard pattern rejects them. Transliterate first, turning "crème brûlée" into "creme-brulee", then validate the result.

Should I validate a slug or generate it from the title?

Both, in order. Generate from the title by lowercasing, replacing non alphanumeric runs with hyphens, and trimming edge hyphens. Then validate the result, which catches empty slugs and other generator edge cases.

Does a passing slug regex guarantee the URL works?

No. The pattern checks shape only. You still need a length cap, a uniqueness check against existing slugs, and a reserved word check so the slug does not collide with app routes like "admin" or "login".

Related reading: A Regex Pattern to Validate a VIN Number: 17 Characters, No I, O, or Q · Regex URL Validation Mistakes · Regex Word Boundaries Explained · Regex for Postal Code Validation

One practical regex guide a week. Get it by email.

Try the tool

Build and test regex patterns with templates for common formats. Build a regex template free, no signup.

Get new free tools by email

Want the next guide in your inbox? I publish one practical guide per new tool. Subscribe to the free newsletter on Substack. No spam, unsubscribe anytime.