Regex:Capturing Groups

No, so the regex finds any* pairs of a lowercase letter ([a-z]) directly followed by an uppercase letter ([A-Z]).

The pattern is /[a-z][A-Z]/, that’s two characters.

So in the string “AllThe-small Things”, there is one instance of that, “lT”:

AllThe-small Things
   ↑
here

The pattern to find each of those is wrapped in brackets

/([a-z])([A-Z])/

That’s two capture groups.

/([a-z])([A-Z])/
   ↑
capture group 1
/([a-z])([A-Z])/
          ↑
capture group 2

Or

/([a-z])([A-Z])/
    ↑       ↑
    $1      $2

Then the second argument to replace is “$1 $2”, so whatever $1 is then a space then whatever $2 is.

In “AllThe-small Things”, the only match is “lT” (lowercase l followed by uppercase T), so for that match, $1 is l and $2 is T, so replace “lT” with “l T”.


* the g flag at the end means global, ie find all instance of the pattern, that’s why it’s any.