summaryrefslogtreecommitdiffstats
path: root/js/src/jit-test/tests/regexp/replace-global-lambda.js
blob: ca102c8d6210a5258a00f927af33aec9833beab7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
"use strict";

function testNoCaptureGroups() {
  var s = "a..bb.cba.";
  for (var i = 0; i < 20; i++) {
    var log = "";
    var res = s.replace(/a|b/g, function(...args) {
      assertEq(this, undefined);
      assertEq(args.length, 3);
      assertEq(args[2], s);
      log += "(" + args[0] + args[1] + ")";
      return "X";
    });
    assertEq(res, "X..XX.cXX.");
    assertEq(log, "(a0)(b3)(b4)(b7)(a8)");
  }
}
testNoCaptureGroups();

function testCaptureGroups() {
  var s = "a..bb.cba.";
  for (var i = 0; i < 20; i++) {
    var log = "";
    var res = s.replace(/(a)|(b)/g, function(...args) {
      assertEq(this, undefined);
      assertEq(args.length, 5);
      assertEq(args[4], s);
      log += "(" + args[0] + args[1] + args[2] + args[3] + ")";
      return "X";
    });
    assertEq(res, "X..XX.cXX.");
    assertEq(log, "(aaundefined0)(bundefinedb3)(bundefinedb4)(bundefinedb7)(aaundefined8)");
  }
}
testCaptureGroups();

// Calling RegExp#compile in the callback has no effect, because |replace| will
// create a new RegExp object from the original source/flags if this happens.
function testCaptureGroupsChanging() {
  var s = "a..bb.cba.";
  for (var i = 0; i < 20; i++) {
    var log = "";
    var re = /a|b/g;
    var res = s.replace(re, function(...args) {
      assertEq(this, undefined);
      assertEq(args.length, 3);
      assertEq(args[2], s);
      log += "(" + args[0] + args[1] + ")";
      re.compile("(a)|(b)", "g");
      return "X";
    });
    assertEq(res, "X..XX.cXX.");
    assertEq(log, "(a0)(b3)(b4)(b7)(a8)");
  }
}
testCaptureGroupsChanging();