🧪 Regex for a slug

This regex ensures that the slug contains only lowercase letters, digits, and hyphens, and does not start or end with a hyphen, nor have consecutive hyphens.

^[a-z0-9]+(?:-[a-z0-9]+)*$
  • ^: Start of the string.

  • [a-z0-9]+: One or more lowercase letters or digits.

  • (?:-[a-z0-9]+)*: Zero or more groups of a hyphen followed by one or more lowercase letters or digits.

  • $: End of the string.