Input string is "thisIsSpinalTap". I replaced each uppercase letter with it's lowercase version. I also wanted to add a - before the letter to get the spinal case effect "this-is-spinal-tap".
I wrote the following line of code:
str = str.replace(/([A-Z])/g, '-').toLowerCase();
This replaced each uppercase letter with - to produce "this-s-pinal-ap". This was not the desired effect.
I read that using $1 could produce the effect I wanted, and it did.
str = str.replace(/([A-Z])/g, '-$1').toLowerCase();
How does the $1 work to append - to the string, rather than replace the uppercase letter?