Subj : lastIndex property of newly created RegExp object To : netscape.public.mozilla.jseng From : Martin Honnen Date : Mon Nov 29 2004 07:01 pm I have run into what I believe to be a bug with the lastIndex property of a newly created RegExp object when looking at the ECMAScript edition 3 specification but as both Spidermonkey (tested with xpcshell of Mozilla 1.7.3) and Rhino (tested with Rhino shell of 1.5R5) behave the same I am a bit puzzled and thought I ask here what others think. Test case looks as follows: function findLine (text) { var linePattern = /.*(\r\n|\r|\n)/g; output('linePattern.lastIndex before exec: ' + linePattern.lastIndex); var match = linePattern.exec(text); output('match: ' + match); } function output (text) { if (typeof document != 'undefined' && document.write) { document.write('

' + text + '

\r\n'); } else if (typeof print != 'undefined') { print(text); } } function test () { var text = 'Line 1\r\nLine 2\r\nLine 3\r\nLine 4'; for (var i = 0; i < 4; i++) { findLine(text); } } test(); Now section 15.10.4.1 of ECMA says that for a newly created RegExp object "The lastIndex property of the newly constructed object is set to 0." so I expect the lastIndex of that linePattern to be 0 on each call of the function findLine but somehow the lastIndex is stored between function calls so Rhino or Spidermonkey output the following: Rhino 1.5 release 5 2004 03 25 js> load('test2004112901.js') linePattern.lastIndex before exec: 0 match: Line 1 , linePattern.lastIndex before exec: 8 match: Line 2 , linePattern.lastIndex before exec: 16 match: Line 3 , linePattern.lastIndex before exec: 24 match: null When I run the example as uploaded here http://home.arcor.de/martin.honnen/mozillaBugs/ecmascript/test2004112902.html with MSIE 6 it always shows 0 for the lastIndex. Any thoughts on why Spidermonkey and Rhino somewhere store and restore the lastIndex property between function calls? -- Martin Honnen http://JavaScript.FAQTs.com/ .