In brief
Expressions with g or y keep a position on the last match.
Useful to know
test() returns true or false to match a regular expression. In flags g and y, the object is stateful and uses lastIndex. For example, const r = /a/g; r.test("a"); r.test("a"); gives sequentially true and false. [1]
For an independent boolean check, you often don't need g. If you use it consciously to iterate, manage the position. Do not explain the changing result as a random error on JavaScript: the same RegExp object continues from the saved place until the task can expect a new search.
Sources
For further detail and to check the information: