summaryrefslogtreecommitdiff
path: root/js/src/tests/ecma_2021/manual/test-replaceall.html
blob: 9f90525493f9dcc1c7e6e5886fc93c54860c51e0 (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
57
58
59
60
61
62
63
<html><head>
<script>
function passfail(test) {
if (test) {
  document.writeln('<span style="color:#008000;">PASS</span>');
} else {
  document.writeln('<span style="color:#BF0000;">FAIL</span>');
}
}
</script>
</head>
<body>
Replace "a" with "b":
<script>
var input='1a234abcd';
var match='a';
var expected='1b234bbcd';
passfail(input.replaceAll(match,'b') === expected);
</script><br>
Replace "a" with "aa":
<script>
var input='aabbccdd';
var match='a';
var expected='aaaabbccdd';
passfail(input.replaceAll(match,'aa') === expected);
</script><br>
Replace "" with "b":
<script>
var input='aaaa';
var match='';
var expected='babababab';
passfail(input.replaceAll(match,'b') === expected);
</script><br>
Replace "old" with special pattern "new (was: $&)":
<script>
var input='This is the old thing of the old time.';
var match='old';
var expected='This is the new (was: old) thing of the new (was: old) time.';
passfail(input.replaceAll(match,'new (was: $&)') === expected);
</script><br>
Replace "/[0-9]/g" with "b":
<script>
var input='1a234abcd';
var match=/[0-9]/g;
var expected='babbbabcd';
passfail(input.replaceAll(match,'b') === expected);
</script><br>
Replace "/[0-9]/" with "b" (Throws):
<script>
var input='1a234abcd';
var match=/[0-9]/;
var expected='1a234abcd';
try {
  test=input.replaceAll(match,'b');
  passfail(false);
} catch(e) {
  passfail(true);
  document.writeln('('+e+')');
}
</script><br>
</body>
</html>