Here’s my one liner solution and very proud of it:
function spinalCase(str) {
// "It's such a fine line between stupid, and clever."
// --David St. Hubbins
return str.replace(/([a-z])([A-Z])/g, "$1 $2").replace(/[\s_-]/g, "-").toLowerCase();
}
spinalCase('This Is Spinal Tap');
Things are starting to fall into place Loving JavaScript more and more everyday!
Hello! This will include all of the possibilities for the algorithm test!
function spinalCase(str) {
var strToChange = str.split(/(?=[A-Z])|\s(?=[a-z])/)
for (var i = 0; i < strToChange.length; i++) {
strToChange[i] = strToChange[i].toLowerCase().replace(/\s|_/g, '');
}
return strToChange.join("-");
}
spinalCase('Teletubbies say Eh-oh The_Andy_Griffith_Show thisIsSpinalTap This Is Spinal Tap AllThe-small Things');
function spinalCase(str) {
// "It's such a fine line between stupid, and clever."
// --David St. Hubbins
return str.replace(/[^a-zA-Z-]/g,"-").replace(/((?=[A-Z])[a-z]*)/g,'-$1').replace(/^-/,"").replace(/--/g,"-").toLowerCase();
}
After solving this task I started understating regex clearly.