NeilFraser / JS-Interpreter

A sandboxed JavaScript interpreter in JavaScript.
Apache License 2.0
1.98k stars 353 forks source link

Regular Expression Test Executes Incorrectly #210

Closed ghost closed 3 years ago

ghost commented 3 years ago
var str = 'table football';

var regex = new RegExp('foo*');
var globalRegex = new RegExp('foo*', 'g');

alert(globalRegex.test(str));
alert(globalRegex.test(str));

This code alerts true and true. Which is incorrect, it's suppose to alert true and false.

Test Mdn Reference

cpcallen commented 3 years ago

I've not looked at the code, but this smells of .lastIndex not being updated correctly.

NeilFraser commented 3 years ago

Confirmed. Here's a minimal test:

var r = /foo*/g;
r.test('table football');
alert(r.lastIndex); 

This should alert 9, but JS-Interpreter alerts 0.