Created
November 28, 2018 04:36
-
-
Save thinhbuzz/a41e5d0934ea697f52607a56273d6924 to your computer and use it in GitHub Desktop.
javascript kebab case convert
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
function kebabCase(str) { | |
return str.replace(/[A-Z\u00C0-\u00D6\u00D8-\u00DE]/g, match => '-' + match.toLowerCase()) // lower case and add - | |
.replace(/[^\w]/g, '-') // replace non-word characters by - | |
.replace(/-+/g, '-') // remove multiple - characters | |
.replace(/^-|-$/g, ''); // trim first and last - character | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment