Reuse Patterns Using Capture Groups- how to understand the rules

Tell us what’s happening:
Hallo, could anyone help me to explain the RegExp rules here, I could not understand the entire contents.
I know $ stands for the end, \s for the space. This totally confuses me.

Thank you in advance!

Your code so far


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

let testString = "test test test test test test";
let reRegex2 =/(test)(\s)\1\2\1/g;
let result2 = reRegex2.test(testString);
console.log(result2);

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

In order:

/
  ^     # start of string
  (\d+) # one or more digits, in capture group
  \s    # a space
  \1    # same as capture group: one or more digits
  \s    # a space
  \1    # same as capture group: one or more digits
  $     # end of string
/

OK,understood, thanks a lot !

1 Like