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 This Pattern
Pattern
^[a-z0-9]+(?:-[a-z0-9]+)*$
^[a-z0-9]+(?:-[a-z0-9]+)*$
Usage
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.
Examples
Matches: hello-world, my-post-123, getting-started-with-react, a. Non-matches: Hello-World, my post, -leading, trailing-
Matches: product-abc, blog-post-title-2024, category/subcategory. Non-matches: 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}.