Skip to content

Instantly share code, notes, and snippets.

@michael-lynch
Created September 12, 2022 21:03
Show Gist options
  • Save michael-lynch/72aece91d6349ad7d381434527152886 to your computer and use it in GitHub Desktop.
Save michael-lynch/72aece91d6349ad7d381434527152886 to your computer and use it in GitHub Desktop.
Convert string to kebab-case
// https://www.w3resource.com/javascript-exercises/fundamental/javascript-fundamental-exercise-123.php
export const toKebabCase = string => {
if (!string) {
return '';
}
const newString = string
.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('-');
return newString;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment