I'm not solving the regex and I have a question

Tell us what’s happening:
Describe your issue in detail here.
My regex is off because it’s not getting rid of the errant dash already present in the string. I want to create a reset wherein I eliminate all punctuation and spaces and replace it with dashes. Where is my logic off? It seems my regex should accomplish this. Spaces (\s) and punctuation like ’ _’ and ’ - ’ are covered by \W. I replace the \W and \s with ‘-’.

Is my understanding of regex off or is it my understanding of how to make regex work?

  **Your code so far**

function spinalCase(str) {
//set regex to deal with underscores and spaces
let regex = /\s\W/g;
//convert to lowercase
let arr = str.toLowerCase();
//use replace to convert underscores, errant dashes and spaces with dashes
arr.replace(regex, '-');;
return str;

spinalCase("AllThe-small Things");
  **Your browser information:**

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

Challenge: Spinal Tap Case

Link to the challenge:

here doesn’t matche because \W match all taht isn’t word.

let regex = /\s\W/g;

and here you are transform to lower case

let arr = str.toLowerCase();

here, you are returnreturn the same str without transform you have return arr;

return str;

you can see result the regex with

console.log(arr.replace(regex, '-'))

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.