Can Anyone Explain. What is the "$1 $2" in first replace statement of solution 1?

str = str.replace(/([a-z])([A-Z])/g, "$1 $2")
                      ↑      ↑
                     $1     $2

So if str is “aB”, that matches (lowercase a-z character followed by uppercase A-Z character), first capturing group is the lowercase, second capturing group is the uppercase, so it will be replaced with “a B”

So if str is “helloThere”, that matches (there is lowercase a-z character followed by uppercase A-Z character), first capturing group is the lowercase, second capturing group is the uppercase, so it will be replaced with “o T”.

So if str is “hello”, that doesn’t match (lowercase a-z characters are only ever followed by another lowercase character)

34 Likes