JavaScriptCore's literal regexs, do not save state (lastIndex) in functions, but other JavaScript implementations do: I believe the relevant section of the of ES3 Specification is: 15.10.6.2 RegExp.prototype.exec(string) Local Regex: ------------ var s='123aaa456'; function foo(a) { return /\d+/g.exec(a); }; [foo(s), foo(s)] Results: jsc: [["123"],["123"]] <-- Different Value v8: [["123"],["456"]] rhino: [["123"],["456"]] spider: [["123"],["456"]] Note that when the regex is not local, but something that stays in scope the entire time, it works as expected. Global Regex: ------------- var s = '123aaa456'; var r = /\d+/g; function foo(a) { return r.exec(a); }; [foo(s), foo(s)] Results are all good: jsc: [["123"],["456"]] v8: [["123"],["456"]] rhino: [["123"],["456"]] spider: [["123"],["456"]]
Brendan isn't our behaviour consistent with that expected from ES5?
(In reply to comment #1) > Brendan isn't our behaviour consistent with that expected from ES5? Oliver, you are correct. I talked to the person (on IRC) that originally found this and he mentioned that this may be ES5 behavior. From the ES5 spec's section on "Changes from ES3" there is: 7.8.5: Regular expression literals now return a unique object each time the literal is evaluated. This change is detectable by any programs that test the object identity of such literal values or that are sensitive to the shared side effects. Old ES3 Text: A regular expression literal is an input element that is converted to a RegExp object (section 15.10) when it is scanned. The object is created before evaluation of the containing program or function begins. Evaluation of the literal produces a reference to that object; it does not create a new object. ES5 Text: A regular expression literal is an input element that is converted to a RegExp object (see 15.10) each time the literal is evaluated. Thanks, I'm closing this as INVALID, because it works correctly.