/^/
EasyProgramming4 min read

kebab-case Identifier Regex

Validates that a string is a lower kebab-case identifier: lowercase words separated by single hyphens, with no leading, trailing, or doubled hyphens.

#kebabcase#programming#identifier#naming-convention#slug

Regex Pattern

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

Pattern Breakdown

Hover over a token to see what it does.

^[a-z][a-z0-9]*(?:-[a-z0-9]+)*$
TokenMeaning
^Anchors the match to the start of the string
[a-z]The identifier must begin with a lowercase letter
[a-z0-9]*Zero or more lowercase letters or digits completing the first word
(?:Start of a non-capturing group for each additional hyphen-separated word
-[a-z0-9]+A hyphen followed by at least one lowercase letter or digit for the next word
)*Closes the group and allows any number of additional words
$Anchors the match to the end of the string

Detailed Explanation

What it does

This pattern checks that a string follows kebab-case naming: it must start with a lowercase letter, and every additional word is introduced by a single hyphen followed by at least one lowercase letter or digit. Strings with uppercase letters, underscores, spaces, or double hyphens are rejected.

Why it works

The leading [a-z][a-z0-9]* requires the identifier to open with a lowercase run, and the repeating group (?:-[a-z0-9]+)* only accepts a hyphen when it's immediately followed by at least one word character, which rules out double hyphens or a trailing hyphen. The ^/$ anchors guarantee the entire string conforms, not just part of it.

Common use cases

  • Validating URL slugs, CSS class names, or npm package names
  • Linting HTML custom element names, which require at least one hyphen
  • Enforcing consistent naming for CLI flag names or config keys
  • Detecting camelCase or snake_case names that should be converted to kebab-case for URLs

Edge cases

  • A single lowercase word with no hyphen, like 'kebab', is valid since the repeating group is optional
  • Trailing digits, like 'page-2', are valid because digits are allowed within any word segment
  • A leading hyphen, like '-leading', is rejected since the first character must be a lowercase letter, not a hyphen
  • Consecutive hyphens, like 'kebab--case', are rejected because a hyphen must be immediately followed by at least one word character

Limitations

  • Does not allow uppercase letters at all, so it can't validate mixed-case identifiers that happen to use hyphens
  • Does not support Unicode letters outside the basic a-z/0-9 ranges, which matters for internationalized slugs
  • Cannot verify that the identifier is unique or meaningful in its target context (e.g. a real URL route or CSS class)

Interactive Tester

Edit the pattern or text below — matching runs live in your browser.

kebab-case my-variable-name kebab

Test Cases

Editable — add your own inputs to see if they pass.

InputExpectedResult
Pass
Pass
Pass
Pass
Pass
Pass
Pass
Pass

Language Variants

Production-ready examples in 12 languages.

const kebabCaseRegex = /^[a-z][a-z0-9]*(?:-[a-z0-9]+)*$/;
console.log(kebabCaseRegex.test('my-variable-name')); // true

Common Mistakes

Writing [a-z0-9-]+ to 'match kebab-case', which also accepts leading/trailing/doubled hyphens like '-foo--bar-'

Fix: Structure the pattern as a required first word plus repeated (?:-[a-z0-9]+)* groups so hyphens must be internal single separators

Not accounting for digits within words, causing valid identifiers like 'step-2' to be rejected

Fix: Include 0-9 in each word's character class, e.g. [a-z0-9]* and [a-z0-9]+

Confusing this with a URL slug pattern that might also need to allow uppercase or Unicode characters

Fix: Add case-insensitivity or Unicode ranges explicitly if the target format is broader than strict ASCII kebab-case

Performance Notes

  • Both quantified segments are non-overlapping character classes, so the engine matches in linear time with no catastrophic backtracking
  • Anchoring with ^ and $ lets invalid identifiers fail fast on the very first character check
  • Cheap enough to run per-keystroke in a URL slug editor or form validator without noticeable cost

Browser Compatibility

EngineSupportedNotes
ChromeYes
FirefoxYes
SafariYes
EdgeYes
Node.jsYes