Why regex test is returning different results for same command [duplicate]

I’m facing a weird behavior by the regex test.

I have the below codes.

const pattern = /[A-Z]/g;
const str = "BehNam";
console.log(pattern.test(str)); //returns true
console.log(pattern.test(str)); //returns true
console.log(pattern.test(str)); //returns false

The first and the second logs return true and the third one returns false.
After a lot of tests, I figured out that this is because I have two Uppercase letters in my string.
Does anyone have any idea why these regex tests lead to different results while I am repeating the same code? And if yes what is the fix?

you can also find the codes here.