PatternRegex
Match a URL slug
Validate kebab-case slugs the way routers expect them: lowercase words separated by single hyphens, never leading, trailing or doubled.
Last updated
Matches
- my-first-post
- v2
- a-1-b-2
Doesn’t match
- -leading
- trailing-
- double--dash
- Has-Caps
Why it’s written this way
One alphanumeric run, then zero or more "-run" groups. Because the hyphen only exists INSIDE the repeating group, a leading hyphen, trailing hyphen or double hyphen has nowhere to match — three validation rules fall out of the structure for free.
The ^…$ anchors make it a whole-field validator; drop them to find slug-shaped runs inside longer text instead.
Edge cases to know
- →Uppercase fails by design — slugify input before validating rather than loosening the pattern.
- →Underscores fail too; if your routes use them, swap the hyphen for [-_].
- →It puts no ceiling on length — enforce that separately if your CMS does.