Regex for URL slug

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

Options
Matches

Examples

Note

Lowercase letters and digits joined by single hyphens, with no hyphen at either end.

Matches in the example

  • "hello-world"
  • "my-post-2024"

Token by token

  1. ^ the start of the string (or line, with the m flag)
  2. [a-z0-9]+ one of: “a” to “z” or “0” to “9”, one or more times, as many as possible
  3. (?: a group, not capturing
  4. - the character “-”
  5. [a-z0-9]+ one of: “a” to “z” or “0” to “9”, one or more times, as many as possible
  6. )* end of group, whole group zero or more times, as many as possible
  7. $ the end of the string (or line, with the m flag)

Other patterns