regex alternates values for #test even though there are no code changes

regex alternates values for #test even though there are no code changes

I’m thoroughly bothered and confused by this. The best way to explain is with the below screen shot.
I’m instantiating the regex object like so:
var PEPPER_STANDARD_DATE_REGEX = /\d{1,2}\/\d{1,2}\/\d{4}, \d{1,2}:\d{1,2} (AM|PM) [A-Z]{1,5}/g

See also: https://www.regex101.com/r/jT2tG4/1

Solutions/Answers:

Solution 1:

This is not bug but a known behavior of g global flag which remembers RegExp.lastIndex position between multiple test or exec calls.

To fix it just remove g flag from your regex i.e.

/\d{1,2}\/\d{1,2}\/\d{4}, \d{1,2}:\d{1,2} (AM|PM) [A-Z]{1,5}/

References

Related:  Ruby doesn't recognize the g flag for regex