Stuck on Spinal Tap Case [SOLVED]

Hi I’ve been working on this one for a few days now. Just not sure how I fix this piece of code to complete the challenge. Any advice/pointers?

function spinalCase(str) {
  var regex = /[\_+ |\s+]/g;

  str = str.replace(/[A-Z]/g, function(match, offset) {  
    return (offset ? '-' : '') + match.toLowerCase();
  });
 
  str = str.replace(regex, '');
 
  return str;
}
 
spinalCase("Teletubbies say Eh-oh");

Did a quick try and my final code does this (pseudocode):

Add space before each capital letter.
Change string to lowercase.
Replace non-letters with space.
Trim spaces (strings have such method).
Replace spaces with dashes.

There are ninjas who can write one line regexes, but I’m not one of them :unamused:

2 Likes

Thanking you! I will give that a go.

EDIT: That’s worked nicely, Thanks @jenovs

I updated your code. Here is my solution for the problem.
function spinalCase(str) {
var re = /(\ba-z)/g;
str = str.replace(re, function(x){return x.toUpperCase();});
var regex = /[-_+ |\s+]/g;
str = str.replace(regex, “”);
str = str.replace(/[A-Z]/g, function(match, offset) {
return (offset ? ‘-’ : ‘’) + match.toLowerCase();
});

return str;
}