freeCodeCamp Challenge Guide: Spinal Tap Case

Hello !

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 :slight_smile: Loving JavaScript more and more everyday!

5 Likes

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');

my solution

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.

1 Like
function spinalCase(str) {
  var words = str.split(/\s|_|(?=[A-Z])/);
  return words.map(function(word){
    return word.toLowerCase();
  }).join('-');
}
1 Like

Ahaa! Finally learnt what these $ signs mean.

function spinalCase(str) {

 return str.replace(/([a-z])([A-Z])/g, "$1 $2").replace(/ +|_/g, "-").toLowerCase();
}

spinalCase('This Is Spinal Tap');
1 Like