Skip to main content
CalcHive

Regex for URL Slug

Matches URL-friendly slugs: lowercase letters and digits separated by single hyphens. No leading/trailing hyphens.

Pattern

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

Pattern Breakdown

TokenExplanation
^Start of string
[a-z0-9]+One or more lowercase letters or digits
(?:-[a-z0-9]+)*Optional repeating: hyphen followed by alphanumeric
$End of string

Test Examples

InputMatch?
my-blog-postMatch
page1Match
a-b-cMatch
-leading-dashNo match
UPPERCASENo match
double--dashNo match

Common Use Cases

  • URL generation
  • Content management systems
  • Blog post URLs
  • SEO-friendly paths

Variations

^[a-z0-9-]+$

Simpler (allows leading/trailing/double hyphens)

Want to test this pattern with your own data?

Open the Regex Tester