Https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/intermediate-algorithm-scripting/spinal-tap-case

Https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/intermediate-algorithm-scripting/spinal-tap-case
0

#1

https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/intermediate-algorithm-scripting/spinal-tap-case

Hi there!

I’m currently going through this particular lesson and I’m finding it difficult to understand one thing - what does ‘$1 $2’ mean here?

  // "It's such a fine line between stupid, and clever."
  // --David St. Hubbins
 var regex = /\s+|_+/g;

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

spinalCase('This Is Spinal Tap');

I’d be grateful for any help!

happy coding :slight_smile:


#2

I don’t actually know. I would recommend using split and join instead.
I never understood regex well.
I still avoid them where-ever possible.
Also, you were the one who put in that piece of code, as it isn’t part of the starting code.


#3

yeah haha, confusing.

I know that ‘$’ pushes things to the end. It’s the code supplied by fcc, this is their explanation:

Code Explanation:
regex contains the regular expression /\s+|_+/g, which will select all white spaces and underscores.

The first replace() puts a space before any encountered uppercase characters in the string str so that the spaces can be replaced by dashes later on.

While returning the string, another replace() replaces spaces and underscores with dashes using regex.


#4

Oh! I get it now!
([a-z]) is a grouping looking for a lowercase letter.
([A-Z]) is a grouping looking for an uppercase letter.
$1 refers to the first grouping, and $2 refers to the second, and because there is a space between them, it inserts a space between a lowercase and an uppercase letter, such as with camelCase.

But I don’t get any of that when I open the challenge myself. Instead, I get this:


#5

Fantastic!

That’s making a lot of sense now hahah! Thanks @Aviage-01 for your help!

P.s. click on Get a hint!


#6

Welcome!
I try to stay away from getting hints, for obvious reasons.