Summary
function spinalCase(str) {
return str.match(/[A-Z][a-z]+|[a-z]+/g)
.join("-")
.toLowerCase();
}
Code Explanation
- Matches all (
g
) the words that begins in uppercase ([A-Z][a-z]+
) or lowercase ([a-z]+
) and puts them in an array. - Join the array using a hyphen (
-
). - Lowercase the whole resulting string.