0

I dont understand the reason for the result of the capturing group in the code below

let regexp = /^((?=(\w+))\2\s?)*$/;

let str = "A good string";

let result = str.match(regexp); 

alert(result[0]); //"A good string"

alert(result[1]); //"string"

alert(result[2]); //"string"

i get the result of the full match but why does the capturing group return only the last word "string" ? As the capturing group follows the anchor ^ in the regexp, i am confused why it is not returning the starting word "A". i cant understand the fundamentals behind the searching mechanism here. Can someone explain this to me ?

Wiktor Stribiżew
  • 484,719
  • 26
  • 302
  • 397

0 Answers0