Web
URL Slug Regex
A slug is the readable part of a URL that identifies a page, such as regex-for-beginners. Good slugs are lowercase, use only letters and digits, and separate words with single hyphens, with no leading, trailing or doubled hyphens. This pattern enforces exactly those rules, which makes it a dependable check for CMS editors, blog platforms, product catalogues and routing configuration. Because it is anchored, the whole value must be a clean slug. The honest limit is that it allows only ASCII, so slugs with accented or non-Latin letters are rejected.
Pattern (JavaScript)
/^[a-z0-9]+(?:-[a-z0-9]+)*$/Starts at the start of the line/string, then one of the characters a-z0-9, one or more times, then (the character "-", then one of the characters a-z0-9, one or more times), zero or more times, then the end of the line/string.
Examples
Matches
- ✓ my-first-post
- ✓ regex-101
- ✓ about
Does not match
- ✗ My-Post
- ✗ my--post
- ✗ -my-post
Try it
0 matches (JavaScript engine)
my-first-post
regex-101
about
My-Post
my--post
-my-postCode in 8 languages
Converted automatically from the JavaScript pattern; language-specific notes are shown where the syntax or support differs.
JavaScript
const re = /^[a-z0-9]+(?:-[a-z0-9]+)*$/g;
const text = "my-first-post";
const matches = [...text.matchAll(re)].map((m) => m[0]);
console.log(matches);Python
import re
pattern = re.compile(r'^[a-z0-9]+(?:-[a-z0-9]+)*$')
text = 'my-first-post'
matches = [m.group(0) for m in pattern.finditer(text)]
print(matches)PHP
<?php
$pattern = '/^[a-z0-9]+(?:-[a-z0-9]+)*$/';
$text = 'my-first-post';
preg_match_all($pattern, $text, $matches);
print_r($matches[0]);Java
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Main {
public static void main(String[] args) {
Pattern pattern = Pattern.compile("^[a-z0-9]+(?:-[a-z0-9]+)*$");
String text = "my-first-post";
Matcher matcher = pattern.matcher(text);
while (matcher.find()) {
System.out.println(matcher.group());
}
}
}Go
package main
import (
"fmt"
"regexp"
)
func main() {
re := regexp.MustCompile(`^[a-z0-9]+(?:-[a-z0-9]+)*$`)
text := "my-first-post"
fmt.Println(re.FindAllString(text, -1))
}C#
using System;
using System.Text.RegularExpressions;
var pattern = new Regex(@"^[a-z0-9]+(?:-[a-z0-9]+)*$");
var text = "my-first-post";
foreach (Match m in pattern.Matches(text))
{
Console.WriteLine(m.Value);
}Rust
// Cargo.toml: regex = "1"
use regex::Regex;
fn main() {
let re = Regex::new(r"^[a-z0-9]+(?:-[a-z0-9]+)*$").unwrap();
let text = "my-first-post";
for m in re.find_iter(text) {
println!("{}", m.as_str());
}
}- Add regex = "1" under [dependencies] in Cargo.toml.
Ruby
pattern = /^[a-z0-9]+(?:-[a-z0-9]+)*$/
text = 'my-first-post'
matches = text.to_enum(:scan, pattern).map { Regexp.last_match[0] }
p matchesFrequently asked questions
- How do I turn a title into a slug?
- Lowercase the title, replace every run of characters outside a-z and 0-9 with a single hyphen, then trim hyphens from both ends. The result will match this pattern.
- Should slugs have a maximum length?
- Shorter slugs are easier to read and share. You can enforce a limit with a separate length check in code, which is clearer than adding it to this pattern.
- Are underscores allowed?
- Not in this pattern. Hyphens are the conventional word separator in URLs, and search engines treat them as word breaks, so the pattern sticks to them.