Reuse Patterns Using Capture Groups - Give me more examples please

Reuse Patterns Using Capture Groups - Give me more examples please
0

#1

I’m struggling to understand this concept and googling for examples of capture groups just leads to finding complicated expressions making it harder.

Can anyone just give some very basic examples of using capture groups including the \number stuff?

My limited understanding with the code writen:

(\d+)\s look for (at least one) numbers until you find a white space
Again, repeat (\d+)\s as requested by challenge
Then repeat three times is denoted by \3

I think the wording of the question is confusing me.

Your code so far





let repeatNum = "42 42 42";
let reRegex = /(\d+)\s(\d+)\s\3/;

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36.

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/regular-expressions/reuse-patterns-using-capture-groups/


#2

It is confusing. It took me quite a while to wrap my head around the concept of capture groups, but if you have patience, it does make sense.

In your regex:
let reRegex = /(\d+)\s(\d+)\s\3/;

…you start out right: the first parenthesis indicates a capture group. Later on IN THAT SAME REGEX, you can refer to that first capture group by \1. The second set of parenthesis, however, are creating a second capture group, which you could refer to as \2.

Instead, where you want to have three instances of the same thing, try something like:

let reRegex = /(\d+)\s\1\s\1/;

This indicates a capture group, whitespace, another instance of the same capture group, whitespace, and a third instance of that same capture group.

It actually took a LONG stretch on a regex tester for me to grok this concept. it’s not easy.

Of course, I haven’t GIVEN you the answer. It will still fail, there are a couple more things you want to be watching for in that regex string, but that will take care of your capture group question…


Reuse Patterns Using Capture Groups_