URL Slug Validation Regex

Validate URL-safe slugs with lowercase letters, numbers, and hyphens. Perfect for SEO-friendly URLs.

Pattern & Test String

About This Pattern

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

Flags: g

Results

Enter a regex pattern to see the results here.

Frequently Asked Questions

What makes a good URL slug?

Good slugs are lowercase, use hyphens (not underscores), contain no special characters, and are descriptive of the content.

Why lowercase only?

Lowercase ensures consistency and prevents duplicate URLs from case variations. Most web servers treat URLs as case-sensitive.

Can slugs start or end with hyphens?

No, this pattern prevents leading/trailing hyphens, which aren't valid in good URL structure.

Practical Examples

Blog Post URLs

Validate slugs for blog posts or articles: /blog/my-article-title

Product Pages

Generate and validate product URL slugs: /products/blue-widget

Category Pages

Validate category slugs for hierarchical URLs.

Common Issues & Solutions

This pattern doesn't prevent consecutive hyphens. Add logic to replace multiple hyphens: slug.replace(/-+/g, '-')

Convert spaces to hyphens and lowercase before validation: str.toLowerCase().replace(/\s+/g, '-')

Remove special characters before creating slugs: str.replace(/[^a-z0-9-]/g, '')

For internationalization, consider allowing Unicode characters with \p{L} instead of a-z (requires 'u' flag).