GUIDE深度 · Deep dive
Regex Flags: g, i, m, s
Flags change how a regex matches, not the expression itself. g finds all, i ignores case, m enables line anchors and s lets dot match newlines; know each flag when combining them.
The four common flags
g: global; without it only the first match is returned.
i: case-insensitive.
m: ^ and $ match each line instead of the whole string.
s: dot also matches newlines; otherwise . does not match \n.
Combination traps
g plus empty matches needs manual lastIndex advancement to avoid loops.
With m, $ is not the string end; use \z or an overall anchor for the end.
FAQ
Does g change exec results?
Yes; g makes exec continue from lastIndex, which suits loops.
Can m and s be combined?
Yes; m handles line anchors, s handles dot across lines.