Index: test/mjsunit/regress/regress-2437.js |
diff --git a/test/mjsunit/regress/regress-2437.js b/test/mjsunit/regress/regress-2437.js |
index 06b69b23db8feae21ac013bf23d2d3fff467bbea..c82293ae3252872c6873b2c0025934eb7d101e98 100644 |
--- a/test/mjsunit/regress/regress-2437.js |
+++ b/test/mjsunit/regress/regress-2437.js |
@@ -25,6 +25,14 @@ |
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
+// Summary of the spec: lastIndex is reset to 0 if |
+// - a regexp fails to match, regardless of global or non-global. |
+// - a global regexp is used in a function that returns multiple results, |
+// such as String.prototype.replace or String.prototype.match, since it |
+// repeats the regexp until it fails to match. |
+// Otherwise lastIndex is only set when a global regexp matches, to the index |
+// after the match. |
+ |
// Test Regexp.prototype.exec |
r = /a/; |
r.lastIndex = 1; |
@@ -73,3 +81,76 @@ r.lastIndex = 1; |
"zzzz".replace(r, function() { return ""; }); |
assertEquals(0, r.lastIndex); |
+// Regexp functions that returns multiple results: |
+// A global regexp always resets lastIndex regardless of whether it matches. |
+r = /a/g; |
+r.lastIndex = -1; |
+"0123abcd".replace(r, "x"); |
+assertEquals(0, r.lastIndex); |
+ |
+r.lastIndex = -1; |
+"01234567".replace(r, "x"); |
+assertEquals(0, r.lastIndex); |
+ |
+r.lastIndex = -1; |
+"0123abcd".match(r); |
+assertEquals(0, r.lastIndex); |
+ |
+r.lastIndex = -1; |
+"01234567".match(r); |
+assertEquals(0, r.lastIndex); |
+ |
+// A non-global regexp resets lastIndex iff it does not match. |
+r = /a/; |
+r.lastIndex = -1; |
+"0123abcd".replace(r, "x"); |
+assertEquals(-1, r.lastIndex); |
+ |
+r.lastIndex = -1; |
+"01234567".replace(r, "x"); |
+assertEquals(0, r.lastIndex); |
+ |
+r.lastIndex = -1; |
+"0123abcd".match(r); |
+assertEquals(-1, r.lastIndex); |
+ |
+r.lastIndex = -1; |
+"01234567".match(r); |
+assertEquals(0, r.lastIndex); |
+ |
+// Also test RegExp.prototype.exec and RegExp.prototype.test |
+r = /a/g; |
+r.lastIndex = 1; |
+r.exec("01234567"); |
+assertEquals(0, r.lastIndex); |
+ |
+r.lastIndex = 1; |
+r.exec("0123abcd"); |
+assertEquals(5, r.lastIndex); |
+ |
+r = /a/; |
+r.lastIndex = 1; |
+r.exec("01234567"); |
+assertEquals(0, r.lastIndex); |
+ |
+r.lastIndex = 1; |
+r.exec("0123abcd"); |
+assertEquals(1, r.lastIndex); |
+ |
+r = /a/g; |
+r.lastIndex = 1; |
+r.test("01234567"); |
+assertEquals(0, r.lastIndex); |
+ |
+r.lastIndex = 1; |
+r.test("0123abcd"); |
+assertEquals(5, r.lastIndex); |
+ |
+r = /a/; |
+r.lastIndex = 1; |
+r.test("01234567"); |
+assertEquals(0, r.lastIndex); |
+ |
+r.lastIndex = 1; |
+r.test("0123abcd"); |
+assertEquals(1, r.lastIndex); |