Files
30-seconds-of-code/snippets/toKebabCase.md
Angelos Chalaris bb722b2eb4 Updated examples
2017-12-27 16:35:25 +02:00

867 B

toKebabCase

Converts a string to kebab case.

Break the string into words and combine them using - as a separator. For more detailed explanation of this Regex, visit this Site.

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('IAmListeningToFMWhileLoadingDifferentURLOnMyBrowserAndAlsoEditingSomeXMLAndHTML') // "i-am-listening-to-fm-while-loading-different-url-on-my-browser-and-also-editing-xml-and-html"