Kebabcase string

JavaScript, String, Regexp · Dec 16, 2020

Converts a string to kebab case.

const toKebabCase = str =>
  str &&
  str
    .match(/[A-Z]{2,}(?=[A-Z][a-z]+[0-9]*|\b)|[A-Z]?[a-z]+[0-9]*|[A-Z]|[0-9]+/g)
    .map(x => x.toLowerCase())
    .join('-');

toKebabCase('camelCase'); // 'camel-case'
toKebabCase('some text'); // 'some-text'
toKebabCase('some-mixed_string With spaces_underscores-and-hyphens');
// 'some-mixed-string-with-spaces-underscores-and-hyphens'
toKebabCase('AllThe-small Things'); // 'all-the-small-things'
toKebabCase('IAmEditingSomeXMLAndHTML');
// 'i-am-editing-some-xml-and-html'

More like this

  • JavaScript String Casing

    Convert between the most common string casing formats with pure JavaScript and a sprinkle of regular expressions.

    Collection · 7 snippets

  • Snakecase string

    Converts a string to snake case.

    JavaScript, String · Jun 28, 2021

  • Titlecase string

    Converts a string to title case.

    JavaScript, String · Oct 22, 2020

  • Pascalcase string

    Converts a string to pascal case.

    JavaScript, String · Sep 8, 2021