Regex Patterns

URL Slug

Match URL-friendly slugs with lowercase letters, digits, and hyphens.

What Is This?

This regex pattern validates URL-friendly slugs (e.g., my-blog-post-title). Slugs must contain only lowercase letters, digits, and hyphens. Hyphens can only separate words — leading, trailing, or consecutive hyphens are not allowed. This is the most common format for SEO-friendly URLs in content management systems.

How to Use

1

The Pattern

Use this pattern to validate slugs generated from post titles, category names, or product names. The slug format ensures clean, readable URLs that are SEO-friendly and work across all browsers and servers. Convert spaces to hyphens and remove special characters before validation.

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

Examples

Example

Valid SEO slugs

Matches:
hello-world
my-post-123
getting-started-with-react
a

Does not match:
Hello-World
my post
-leading
trailing-
Example

Various formats

Matches:
product-abc
blog-post-title-2024
category/subcategory

Does not match:
double--hyphen
UPPERCASE
special!chars

Related Entries

More from this reference:

Frequently Asked Questions

Should I allow underscores in slugs?

Hyphens are preferred for SEO. Google treats hyphens as word separators but underscores as word joiners. For example, 'my-post' is seen as two words while 'my_post' is one word.

What about Unicode characters in slugs?

Modern CMS platforms support Unicode slugs (e.g., Chinese, Arabic). This ASCII-only pattern would reject them. For multilingual sites, use a broader pattern that allows Unicode word characters with \p{L}.