DD
DevDash

Regex for URL Slug

Regex Pattern

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

Lowercase URL-friendly slug with dashes

Quick Answer

The regex pattern for url slug is `^[a-z0-9]+(?:-[a-z0-9]+)*$`. Lowercase URL-friendly slug with dashes. This works in JavaScript, Python, Ruby, PHP, Java, and most regex engines that support PCRE syntax.

Test Examples

InputResult
my-blog-post✓ Matches
hello✓ Matches
article-2026-final✓ Matches
My-Post✗ No match
hello world✗ No match
-leading-dash✗ No match
trailing-✗ No match

Code Examples

javascript

const regex = /^[a-z0-9]+(?:-[a-z0-9]+)*$/;
const isValid = regex.test(value);

python

import re
pattern = r'^[a-z0-9]+(?:-[a-z0-9]+)*$'
if re.match(pattern, value):
    print("valid")

ruby

pattern = /^[a-z0-9]+(?:-[a-z0-9]+)*$/
if value =~ pattern
  puts "valid"
end

php

if (preg_match('/^[a-z0-9]+(?:-[a-z0-9]+)*$/', $value)) {
    echo "valid";
}

java

String pattern = "^[a-z0-9]+(?:-[a-z0-9]+)*$";
boolean isValid = value.matches(pattern);

Frequently Asked Questions

Related Regex Patterns

Want API access + no ads? Pro coming soon.