*Intermediate Algorithm Scripting: Spinal Tap Case

Tell us what’s happening:
I do not know what is the reason why my regex is wrong. I do not know how to set for replacing with whitespaces before capital letters and replace whitespaces with - at the same time.

Your code so far


function spinalCase(str) {

let replacedWhiteSpaceStr=str.replace(/_([A-Z])/g, " ").trim().toLowerCase().split(" ").join("-")

console.log( replacedWhiteSpaceStr)
   
   return replacedWhiteSpaceStr;
}

spinalCase("Teletubbies say Eh-oh");

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.122 Safari/537.36.

Challenge: Spinal Tap Case

Link to the challenge:

In the case of an underscore followed by an uppercase letter, you replace both the underscore and the letter with a single space (deleting the letter). Your solution also does not add hyphens (-) between camelcased characters.

1 Like

It is done. Thanks!

function spinalCase(str) {
  
  let replacedWhiteSpaceStr=str.replace(/([A-Z])/g," $1").trim().replace(/ +/g, '-').toLowerCase().replace(/_/g, '')

console.log( replacedWhiteSpaceStr)
     
     return replacedWhiteSpaceStr;
}

Congratulations! Happy coding.

1 Like

i found out that solution also just match the words

It is great that you solved the challenge, but instead of posting your full working solution, it is best to stay focused on answering the original poster’s question(s) and help guide them with hints and suggestions to solve their own issues with the challenge.

We are trying to cut back on the number of spoiler solutions found on the forum and instead focus on helping other campers with their questions and definitely not posting full working solutions.

In this case, the question was asked 3 months ago and the original poster probably does not need help with this challenge anymore.

Thank you for understanding.

1 Like

okay sorry i understand