I’ve regex a(boy)?(boycott)?
testing against aboycott
.
const result = "aboycott".match(/a(boy)?(boycott)?/);
console.log(result[0]);
My thought process
a
matches witha
b
matches withb
o
matches witho
y
matches withy
c
doesn't matches withb
Since (boy) is optional so its state is saved and the REGEX engine can backtrack to try the saved state
b
matches withb
o
matches witho
y
matches withy
c
matches withc
o
matches witho
t
matches witht
t
matches witht
The final match should be aboycott
but it matches aboy
. What is going on here? If I'm not wrong ?
is greedy it first tries to match and the state is saved. If there is no match then backtrack to the saved place.