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

In the test case of spinalCase(“Teletubbies say Eh-oh”), your regular expression checks for a pattern where character is not A-Z followed by one or more characters which are not a-z. So, the first instance of this pattern is the “s” (1st capture group) on the end of “Teletubbies” and the next space character (2nd capture group), so it replaces "s " with "s- “. Next, the regex pattern matches the space (1st capture group) and the letter “E” (2nd capture group) in “Eh-oh”, so it replaces " E” with “-E”. Finally, the regex patterns finds “h” (1st capture group) at the end of “Eh” and the “-” (2nd capture group) in “-oh”, so it replaces “h-” with “h–”.

Then you call toLowerCase on the returned value of the replace method which makes all letters lower case and creates the following:

"Teletubbies- say-Eh--oh"

1 Like

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 @RandellDawson!

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!