Slug Validation Regex (URL Friendly)
• CronOS Team
regexslugvalidationtutorialurl-friendly
Need to generate a regex pattern?
Use CronOS to generate any regex pattern you wish with natural language. Simply describe what you need, and we'll create the perfect regex pattern for you. It's completely free!
Slug Validation Regex (URL Friendly)
Validate URL-friendly slugs with lowercase alphanumeric characters and hyphens using regex pattern.
Pattern Breakdown
regex
^[a-z0-9]+(?:-[a-z0-9]+)*$
Components
| Component | Description | Matches |
|---|---|---|
^ | Start anchor | Ensures match from string start |
[a-z0-9]+ | First segment | One or more lowercase letters or digits |
(?:-[a-z0-9]+)* | Optional segments | Zero or more hyphen-separated segments |
$ | End anchor | Ensures match to string end |
Detailed Breakdown
[a-z0-9]+- First segment: must start with letter or digit(?:-[a-z0-9]+)*- Optional additional segments:(?:...)- Non-capturing group-- Literal hyphen separator[a-z0-9]+- One or more lowercase letters or digits*- Zero or more occurrences
Examples
Valid:
hello-worldmy-awesome-postarticle123test-slug-2024a123abc
Invalid:
Hello-World(uppercase not allowed)-hello-world(starts with hyphen)hello-world-(ends with hyphen)hello--world(double hyphen)hello world(spaces not allowed)hello_world(underscores not allowed)hello.world(dots not allowed)
Implementation
JavaScript
javascript
const slugRegex = /^[a-z0-9]+(?:-[a-z0-9]+)*$/;
slugRegex.test('hello-world'); // true
slugRegex.test('my-awesome-post'); // true
slugRegex.test('Hello-World'); // false (uppercase)
slugRegex.test('-hello-world'); // false (starts with hyphen)
slugRegex.test('hello--world'); // false (double hyphen)
Python
python
import re
slug_regex = r'^[a-z0-9]+(?:-[a-z0-9]+)*$'
bool(re.match(slug_regex, 'hello-world')) # True
bool(re.match(slug_regex, 'my-awesome-post')) # True
bool(re.match(slug_regex, 'Hello-World')) # False (uppercase)
Go
go
slugRegex := regexp.MustCompile(`^[a-z0-9]+(?:-[a-z0-9]+)*$`)
slugRegex.MatchString("hello-world") // true
slugRegex.MatchString("my-awesome-post") // true
slugRegex.MatchString("Hello-World") // false (uppercase)
Limitations
- Lowercase only: Only accepts lowercase letters
- No underscores: Doesn't allow underscores
- No special chars: Only hyphens allowed as separators
- No length limit: Doesn't enforce maximum length
- No reserved words: Doesn't prevent reserved slug names
When to Use
- URL slug generation and validation
- Blog post URLs
- SEO-friendly URLs
- Content management systems
- When you need clean, readable URLs
For production, consider:
- Adding maximum length validation
- Checking against reserved slug names
- Supporting case-insensitive matching
- Handling special characters by conversion
- Normalizing input before validation
Need to generate a regex pattern?
Use CronOS to generate any regex pattern you wish with natural language. Simply describe what you need, and we'll create the perfect regex pattern for you. It's completely free!