将字符串转换为 spinal case。Spinal case 是 all-lowercase-words-joined-by-dashes 这种形式的,也就是以连字符连接所有小写单词。
代码:
function spinalCase(str) {
// "It's such a fine line between stupid, and clever."
// --David St. Hubbins
str = str.replace(/([A-Z]+)/g,' $1');
str = str.replace(/\s+|_+/g,'-');
if(str[0] == "-"){str = str.substring(1);}
str = str.replace(/--/g,'-');
str = str.toLowerCase();
return str;
}
spinalCase("thisIsSpinalTap");