Created
September 12, 2022 21:03
-
-
Save michael-lynch/72aece91d6349ad7d381434527152886 to your computer and use it in GitHub Desktop.
Convert string to kebab-case
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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