Skip to content

Conversation

@s-belichenko
Copy link

In some cases, this code will not work correctly. Calling these methods on the same RegExp, will start checking from the end of the previous comparison.

Just try to run this code:

var regex1 = RegExp('foo*');
var regex2 = RegExp('foo*','g');
var str1 = 'table football';

console.log(regex1.exec(str1));
console.log(regex1.exec(str1));
console.log(regex2.exec(str1));
console.log(regex2.exec(str1));
console.log(regex2.exec(str1));
console.log('==============');
console.log(str1.match(regex1));
console.log(str1.match(regex1));
console.log(str1.match(regex2));
console.log(str1.match(regex2));
console.log(str1.match(regex2));

Result:

Array ["foo"]
Array ["foo"]
Array ["foo"]
null
Array ["foo"]
"=============="
Array ["foo"]
Array ["foo"]
Array ["foo"]
Array ["foo"]
Array ["foo"]

@s-belichenko s-belichenko changed the title Don't use exec() or test() here additional/pattern.js: Don't use exec() or test() here Aug 13, 2018
In some cases, this code will not work correctly. Calling these methods on the same RegExp, will start checking from the end of the previous comparison.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants