The fines calculator at the library kiosk: exec() again and again from lastIndex. What do g and y do when they meet a character they do not know? The base of…
Why it matters
Whenever you write a parser or tokenizer of your own (a mini language, formulas, a highlighter, templates), you need "the match must be right here". A g that likes to jump lets broken input through silently. y makes "no match" mean an error at this very place.
The key idea
y anchors every try at lastIndex and refuses to jump
Try it in this playground
Hit a wall with y Step exec() until null in the middle of the text: the × on the board, where the syntax error is.
Watch g jump The "jump" ring on the board: what exec() passed over to find the next match.
Start in the middle Move the starting lastIndex in front of a token and use y: the first exec() gets it at once.
Make a loop that never ends Switch every flag off: lastIndex does not move, exec() repeats the same match.