LarryBattle / Ratio.js

Rational numbers for Javascript
http://larrybattle.github.com/Ratio.js/
MIT License
113 stars 9 forks source link

Possible regex malfunction #36

Closed Nioub closed 11 years ago

Nioub commented 11 years ago

I haven't properly analyzed yet, but I think there may be a bug with Ratio.regex if these regexes are being reused, their lastIndex property is not being reset to 0 in subsequent test() calls (see http://stackoverflow.com/questions/1534098/regex-lastindex-unexpected-behaviour). This can be fixed easily for example by manually reseting before use:

    myRegex.lastIndex = 0;
    if (myRegex.test(myString)) ...
LarryBattle commented 11 years ago

Thanks for notifying me about this issue. The problem is that if a cached RegExp contains a set global flag g, then the lastIndex property on the object will only reset to 0 when a failed match has occurred for .exec() or .test(). However, this isn't a problem with Ratio.js because there isn't an RegExp objects that uses the global flag g. If there was one, then I would have to create a new RegExp every time the function was called.

More info here: http://www.tutorialspoint.com/javascript/regexp_lastindex.htm

Nioub commented 11 years ago

Thanks for the insight. I did not realized this was caused by the g flag. I'll check my codebases for misuses of this flag.