Capture Groups confusing patterns

Tell us what’s happening:
my code should mean:

  1. Find a 1 or more digits followed by a space
  2. Repeat (1.) 2 times

The above interpretation is not right and i do not get it (i mean why it is wrong?).

Your code so far


let repeatNum = "42 42 42";
let reRegex = /(\d+\s)\2/; // Change this line
let result = reRegex.test(repeatNum);
console.log(result);

Second i do not understand the last two hints in the lessons; speaking specifically.

Number 2:

let testString = "test test test";
let reRegex = /(test)(\s)\1\2\1/;
let result = reRegex.test(testString);

I do not understand specifically \1 & \2 & \1\ & finally why did he use global?

Number 3:

let testString = "test test test test test test";
let reRegex = /(test)(\s)\1\2\1/g;
let result = reRegex.test(testString);

I do not understand specifically why did he wrote \s\1\s\1$ because (from what i understand) this means 4 spaces at the end.

Your browser information:

User Agent is: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.66 Safari/537.36.

Challenge: Reuse Patterns Using Capture Groups

Link to the challenge:

the \1 refers to the first capture group, the \2 refers to the second capture group - the capture groups are numbered and you can refer them with their number like

good question, it is unnecessary and cause unwanted consequences
where did you found it?

Execuse me and please be patient as i still did not understand the last part; there is two \1 and one \2; yo explained only the first \1\2 and not the last \1.

Extremely sorry for the delayed/late reply. in the solution provided in the hints section.

I did, \1 refers to the first capture group, it is used twice, so there is two references to the first capture group

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