Back to Home

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!

Generate Regex Pattern

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

ComponentDescriptionMatches
^Start anchorEnsures match from string start
[a-z0-9]+First segmentOne or more lowercase letters or digits
(?:-[a-z0-9]+)*Optional segmentsZero or more hyphen-separated segments
$End anchorEnsures 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-world
  • my-awesome-post
  • article123
  • test-slug-2024
  • a
  • 123abc

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

  1. Lowercase only: Only accepts lowercase letters
  2. No underscores: Doesn't allow underscores
  3. No special chars: Only hyphens allowed as separators
  4. No length limit: Doesn't enforce maximum length
  5. 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!

Generate Regex Pattern