Spinal Tap Case - Parenthesized Substring Match

My code is passing the first three tests, however I’m encountering issues when trying to remove the the dashes and spaces that are already present. I’m using parenthesized substring matches in order to return only the upper and lower case letters and combining with the dash, however I’m still getting dashes and spaces from the initial string. Any ideas of where my logic is wrong or if you could push me in the correct directions would be greatly appreciated. Thanks!


function spinalCase(str) {
const re = /([^A-Z])([^a-z])+/g;
return str.replace(re,"$1-$2").toLowerCase();
}
spinalCase('This Is Spinal Tap');

So then I would need to look for a lowercase character followed by either an uppercase or space/dash?However, should I be adding a third placeholder to account for spaces/dashes? I’m fuzzy on how parenthesized substring matches operate.

Hmmmmnnnnn, I’m still having issues with the code. Utilizing parenthesized substring matches is the correct way to go about solving the problem, correct? I’m wondering if I have just the wrong regular expression combination or if my logic is fundamentally wrong. Thanks again for the help @camperextraordinaire!

Hello pbculley, I got the solution.

All we need to do is replace unwanted part of string with what we need.

spinalCase(“thisIsSpinalTap”) – In this case we can use $operator in regular expression. Two ranges of characters, a-z and A-Z. Characters from these ranges are grouped together. So we need to identify the two characters which are from taken from two respective ranges(For example: “sI”, “sS” and “lT”) .
Then we need to separate these two characters with a space " ". Which we can do by using $operator

str.replace(/([a-z])([A-Z])/g, '$1 $2');

And in the next step, we need to identify the characters which are not alphabets and digits, which we can obtain by using regular expression /W, but it includes _ underscore. So to eliminate underscore we should except it from the regexp. Which we can by using | or Operator.
Then we need to replace these characters with - hyphen.

str.replace(/\W|_/g, "-");

Finally we convert the string to lower case using .toLowerCase() method.

My Solution is:

function spinalCase(str) {
var newArr= str.replace(/([a-z])([A-Z])/g, ‘$1 $2’).replace(/\W|_/g, “-”);
str = newArr.toLowerCase();
return str;
}
spinalCase(‘This Is Spinal Tap’);

@Bhagathp Thanks! I think where I was trying too hard was in making this a one-liner which I don’t believe is possible. I had thought about actually using replace twice, so your suggestion worked! I must have tried close to every combination; at that point I wasn’t even learning anything but merely guessing. Thanks again for all your help!