Skip to content

Instantly share code, notes, and snippets.

@tak-bro
Created October 7, 2020 04:25
Show Gist options
  • Select an option

  • Save tak-bro/6515fdf6bd90d0ed24dac902d5c6d71d to your computer and use it in GitHub Desktop.

Select an option

Save tak-bro/6515fdf6bd90d0ed24dac902d5c6d71d to your computer and use it in GitHub Desktop.
[JS] convert string to kebab case
// from: 30 Seconds of Knowledge
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"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment