A small problem with the g parameter of js regular expression

clipboard.png

as shown in the figure above: in the first way, the "$3.23" is identified as false , but in the second way, only one parameter g is removed and passed. According to principle, the first one should also be passed, please give us guidance!


test is used repeatedly. If g is used, the next match will start from the next index of the previous match, so the starting point for the second use of test should be .32, so it becomes false.
the following is mentioned:
https://codeshelper.com/a/11.


// 
var reg = /\w+/g;
reg.test('ab'); // true
console.log(reg.lastIndex); // 2
// false
reg.test('ab'); // false
console.log(reg.lastIndex); // 0
// true
reg.test('ab'); // true
console.log(reg.lastIndex); // 2
// falseg
reg.test('ab'); // false

/ / MDN regularization has this knowledge point. But it doesn't seem to open now.

7.2.4. The modifier g affects exex and test .
the lastIndex attribute of the regular instance means that when trying to match, the match starts with the lastIndex bit of the string. The four methods of
strings start with 0 every time they match, that is, the lastIndex attribute is always the same.
while the two methods of the regular instance exec and test , when the regular is a global match, the lastIndex will be modified after each matching is completed.

the above paragraph is excerpted from " JavaScript regular expression Mini Book ", recommended.

Menu