# Regex:Capturing Groups

**URL:** <https://forum.freecodecamp.org/t/regex-capturing-groups/449527>\
**Category:** Curriculum Help\
**Tags:** javascript\
**Created:** [March 4, 2021, 7:13am UTC](https://forum.freecodecamp.org/t/regex-capturing-groups/449527 "2021-03-04T07:13:55Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![cihatsaman](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/cihatsaman/32/135759_2.png) [@cihatsaman](https://forum.freecodecamp.org/u/cihatsaman)\
**Post date:** [March 4, 2021, 7:13am UTC](https://forum.freecodecamp.org/t/regex-capturing-groups/449527/1 "2021-03-04T07:13:55Z")

</div>

```auto
let str = "AllThe-small Things"
str = str.replace(/([a-z])([A-Z])/g, "$1 $2");
console.log(str); 
**// output is:All The-small Things**

```

\*What difference between those two code blocks? Normally, I know I have 3 capture groups on that sample. The one is for lower case the other is for uppercase and the last one is for the main string. But, I didn’t understand that groups clearly.What I am missing here? \*

```auto
let str = "AllThe-small Things"
str = str.replace(/([a-z])([A-Z])/g, "$2 $1");
console.log(str); 
**// output is:AlT lhe-small Things**

```

---

<div class="post-metadata">

**Author:** ![ILM](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/ilm/32/363239_2.png) [@ILM](https://forum.freecodecamp.org/u/ILM)\
**Post date:** [March 4, 2021, 8:00am UTC](https://forum.freecodecamp.org/t/regex-capturing-groups/449527/2 "2021-03-04T08:00:10Z")

</div>

there is only two capture groups, what’s the third one you are referring to?

---

<div class="post-metadata">

**Author:** ![DanCouper](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/dancouper/32/158608_2.png) [@DanCouper](https://forum.freecodecamp.org/u/DanCouper)\
**Post date:** [March 4, 2021, 8:41am UTC](https://forum.freecodecamp.org/t/regex-capturing-groups/449527/3 "2021-03-04T08:41:41Z")

</div>

> [@Can Anyone Explain. What is the "$1 $2" in first replace statement of solution 1?](https://forum.freecodecamp.org/t/can-anyone-explain-what-is-the-1-2-in-first-replace-statement-of-solution-1/352574/7):
>
> 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 th…

---

<div class="post-metadata">

**Author:** ![cihatsaman](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/cihatsaman/32/135759_2.png) [@cihatsaman](https://forum.freecodecamp.org/u/cihatsaman)\
**Post date:** [March 4, 2021, 1:13pm UTC](https://forum.freecodecamp.org/t/regex-capturing-groups/449527/4 "2021-03-04T13:13:45Z")

</div>

$0 that one whole string

---

<div class="post-metadata">

**Author:** ![ILM](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/ilm/32/363239_2.png) [@ILM](https://forum.freecodecamp.org/u/ILM)\
**Post date:** [March 4, 2021, 1:22pm UTC](https://forum.freecodecamp.org/t/regex-capturing-groups/449527/5 "2021-03-04T13:22:12Z")

</div>

that’s not a capture group, that is the new text that is being inserted in the string in place of what the regex matched

`$1` and `$2` let you use what was captured by the capture groups in the new string, `$1` is a placeholder for what the first capture group has captured, and `$2` for the second one

in the string the regex matches `lT`, where `l` is captured by the first group and `T` by the second  
`"$1 $2"` results in `"l T"` and `"$2 $1"` in `"T l"`

---

<div class="post-metadata">

**Author:** ![cihatsaman](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/cihatsaman/32/135759_2.png) [@cihatsaman](https://forum.freecodecamp.org/u/cihatsaman)\
**Post date:** [March 4, 2021, 1:26pm UTC](https://forum.freecodecamp.org/t/regex-capturing-groups/449527/6 "2021-03-04T13:26:37Z")

</div>

You mean ,first capturing groups should be lower case and second group should be upper case for first case code.(str.replace(/([a-z])([A-Z])/g, “$1 $2”). if str is “AllThe-small Things” for first case I should to find lower cases ? Because for “All” the ‘A’ is uppercase but still it can come first with group. I confused so much 🙂 Thanks…

---

<div class="post-metadata">

**Author:** ![cihatsaman](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/cihatsaman/32/135759_2.png) [@cihatsaman](https://forum.freecodecamp.org/u/cihatsaman)\
**Post date:** [March 4, 2021, 1:51pm UTC](https://forum.freecodecamp.org/t/regex-capturing-groups/449527/7 "2021-03-04T13:51:00Z")

</div>

Thanks for the quick answer. I understood now. I was confused about that white space 🙂 between those groups($1 $2) has also space I have missed that white space.

---

<div class="post-metadata">

**Author:** ![DanCouper](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/dancouper/32/158608_2.png) [@DanCouper](https://forum.freecodecamp.org/u/DanCouper)\
**Post date:** [March 4, 2021, 2:45pm UTC](https://forum.freecodecamp.org/t/regex-capturing-groups/449527/8 "2021-03-04T14:45:40Z")

</div>

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”:

```auto
AllThe-small Things
   ↑
here

```

The pattern to find each of those is wrapped in brackets

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

```

That’s two _capture groups_.

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

```

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

```

Or

```auto
/([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.

---

<div class="post-metadata">

**Author:** ![cihatsaman](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/cihatsaman/32/135759_2.png) [@cihatsaman](https://forum.freecodecamp.org/u/cihatsaman)\
**Post date:** [March 4, 2021, 3:43pm UTC](https://forum.freecodecamp.org/t/regex-capturing-groups/449527/9 "2021-03-04T15:43:35Z")

</div>

Thank you , I really appreciate it for your great explanation … I totaly understood 🙂

---

<div class="post-metadata">

**Author:** ![system](https://global.discourse-cdn.com/freecodecamp/original/3X/e/1/e1c433102e34b46dfaa3e15d13ab9a514b127078.svg) [@system](https://forum.freecodecamp.org/u/system)\
**Post date:** [September 3, 2021, 3:43am UTC](https://forum.freecodecamp.org/t/regex-capturing-groups/449527/10 "2021-09-03T03:43:57Z")

</div>

Closed on September 3, 2021, 3:43am UTC

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.
