DevTools
Back to Regex Tester

URL Slug Regex Pattern

Match and validate URL-friendly slugs using regex. Ensures lowercase letters, digits, and hyphens only, with no leading or trailing hyphens.

The Pattern

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

How It Works

TokenMeaning
^Start of string
[a-z0-9]+One or more lowercase letters or digits
(?:-[a-z0-9]+)*Zero or more groups of a hyphen followed by lowercase letters/digits
$End of string

Test Cases

hello-worldMatch
my-blog-post-123Match
simpleMatch
a-b-cMatch
Hello-WorldNo match
-leading-hyphenNo match
trailing-hyphen-No match
double--hyphenNo match

Code Examples

javascript
const slugRegex = /^[a-z0-9]+(?:-[a-z0-9]+)*$/;
const slug = "my-blog-post-123";
const isValid = slugRegex.test(slug);
console.log(isValid); // true
python
import re
slug_regex = r'^[a-z0-9]+(?:-[a-z0-9]+)*$'
slug = "my-blog-post-123"
is_valid = bool(re.match(slug_regex, slug))
print(is_valid)  # True

Common Use Cases

Related Patterns

Want to test this pattern with your own data?

Try this pattern in Regex Tester →