Spinal Tap Case
What is "Spinal case" ?所謂的spinal case就是用小寫跟dash(-)連結所有的單字,Ex: all-lowercase-words-joined-by-dashes這樣的形式
function spinalCase(str) {
// "It's such a fine line between stupid, and clever."
// --David St. Hubbins
var regex = /[A-Z]/g;
return (str[0] == str[0].toUpperCase()) ? str.replace(/[\s]/g, '-').replace(/_/g, '-').toLowerCase():
str.replace(regex,'-' + '$&').toLowerCase();
}
spinalCase('This Is Spinal Tap');
spinalCase("thisIsSpinalTap");
spinalCase("The_Andy_Griffith_Show");
spinalCase("Teletubbies say Eh-oh");
Solution:
P.S. / Reference: String.prototype.replace()
RegExp