I’ve regex a(boy)?(boycott)? testing against aboycott.
const result = "aboycott".match(/a(boy)?(boycott)?/);
console.log(result[0]);
My thought process
amatches withabmatches withbomatches withoymatches withycdoesn't matches withb
Since (boy) is optional so its state is saved and the REGEX engine can backtrack to try the saved state
bmatches withbomatches withoymatches withycmatches withcomatches withotmatches withttmatches 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.