I’ve regex  a(boy)?(boycott)? testing against aboycott.
const result = "aboycott".match(/a(boy)?(boycott)?/);
console.log(result[0]);My thought process
- amatches with- a
- bmatches with- b
- omatches with- o
- ymatches with- y
- cdoesn't matches with- b
Since (boy) is optional so its state is saved and the REGEX engine can backtrack to try the saved state
- bmatches with- b
- omatches with- o
- ymatches with- y
- cmatches with- c
- omatches with- o
- tmatches with- t
- tmatches with- t
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.
