I need to regex string with a year inside. Template is 'Year-<4 digits>-"high OR low"-level'.
I've built this regex: /Year-\d{4}-\b(low|high)\b-level/gi; In online regex testers my strings pass the check. Sample code:
const template = /Year-\d{4}-\b(low|high)\b-level/gi;
const txtArr = ['Year-2019-low-level', 'Year-2019-high-level', 'Year-low-level', 'Year-high-level', 'Year-2018-low-level', 'Year-2018-low-level']
for (const s of txtArr) {
console.log(template.test(s), s);
}
I expect 2 of sample strings to not pass, but 4 should pass. But they dont - only 2 of them pass. Also in browser console they don't pass. Tried in FF and Chrome. Can't understand why. Also, if I copy the string that is not passing the match and just make
console.log(template.test('Year-2018-low-level'), 'Year-2018-low-level');
it passes! I've got only one idea: looks like in every iteration of loop something is not reset in regex, and it keeps something in memory, that is not letting match pass.
P.S. I even copied the same string which must pass the test to array, like that:
const txtArr = ['Year-2019-low-level', 'Year-2019-low-level', 'Year-2019-low-level', 'Year-2019-low-level', 'Year-2019-low-level', 'Year-2019-low-level']
and the results are true-false-true-false-true... Why? And how to fix?