I have string and want to capture a fragment by regex as:
var regex = new RegExp("(\s?.{0,2}the.{0,2}\s?)", "i");
var str= " the apple the apple the apple the apple the apple the apple the apple"
alert(str.match(regex))
The goal is to capture the first “max 3 symbol with(within) space” + “the” + “max 3 symbol with(within) space”
I dont understand why result is duplicated ——> the a, the a
match will handle the capturing group around the expression for you.
Your regex should be: