Okay, I don’t know what level of “not understanding” you have, so I apologize if I tell you something you do understand. .
([A-Z]) are capture groups. Each
(something inside parens) tells the regex you want to save the match. And the
g a the end tells regex you want to do it for all the matches in your string, not just the first one (
g stands for global here)
You are telling regex that you want to catch a lowercase character a thru z and save it and you want to do the same for an uppercase character A thru Z. but because they are side by side like this
([a-z])([A-Z]) you want it to capture a lowercase a thru z that is right before an uppercase A thru Z and an uppercase A thru Z that is right after a lowercase a thru z. Following?
So, now we have our two capture groups, what do we do with them? We are going to use the
String.replace() string method to change
$1 is like a variable referring to your first capture group in you regex and
$2 refers to your second group and so on. You could say that before the replace did its thing your capture groups looked like this in
'$1$2'. They are right next to each other cause that is what you were grabbing as I explained in the second paragraph. But you are telling
String.replace() that you want to replace that with
'$1 $2', putting a space between them.
'iAmCamelCase'.replace(/([a-z])([A-Z])/g, '$1 $2'); would equal
'i Am Camel Case'.
I hope that helped.