OLD | NEW |
1 // Copyright 2012 the V8 project authors. All rights reserved. | 1 // Copyright 2012 the V8 project authors. All rights reserved. |
2 // Redistribution and use in source and binary forms, with or without | 2 // Redistribution and use in source and binary forms, with or without |
3 // modification, are permitted provided that the following conditions are | 3 // modification, are permitted provided that the following conditions are |
4 // met: | 4 // met: |
5 // | 5 // |
6 // * Redistributions of source code must retain the above copyright | 6 // * Redistributions of source code must retain the above copyright |
7 // notice, this list of conditions and the following disclaimer. | 7 // notice, this list of conditions and the following disclaimer. |
8 // * Redistributions in binary form must reproduce the above | 8 // * Redistributions in binary form must reproduce the above |
9 // copyright notice, this list of conditions and the following | 9 // copyright notice, this list of conditions and the following |
10 // disclaimer in the documentation and/or other materials provided | 10 // disclaimer in the documentation and/or other materials provided |
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
79 assertEquals("bc", RegExp.lastMatch); | 79 assertEquals("bc", RegExp.lastMatch); |
80 assertEquals("c", RegExp.lastParen); | 80 assertEquals("c", RegExp.lastParen); |
81 assertEquals(undefined, RegExp.lastIndex); | 81 assertEquals(undefined, RegExp.lastIndex); |
82 assertEquals(undefined, RegExp.index); | 82 assertEquals(undefined, RegExp.index); |
83 assertEquals("d", RegExp.rightContext); | 83 assertEquals("d", RegExp.rightContext); |
84 assertEquals('b', RegExp.$1); | 84 assertEquals('b', RegExp.$1); |
85 assertEquals('c', RegExp.$2); | 85 assertEquals('c', RegExp.$2); |
86 for (var i = 3; i < 10; i++) { | 86 for (var i = 3; i < 10; i++) { |
87 assertEquals("", RegExp['$' + i]); | 87 assertEquals("", RegExp['$' + i]); |
88 } | 88 } |
| 89 |
| 90 |
| 91 function Override() { |
| 92 // Set the internal lastMatchInfoOverride. After calling this we do a normal |
| 93 // match and verify the override was cleared and that we record the new |
| 94 // captures. |
| 95 "abcdabcd".replace(/(b)(c)/g, function() { }); |
| 96 } |
| 97 |
| 98 |
| 99 function TestOverride(input, expect, property, re_src) { |
| 100 var re = new RegExp(re_src); |
| 101 var re_g = new RegExp(re_src, "g"); |
| 102 |
| 103 function OverrideCase(fn) { |
| 104 Override(); |
| 105 fn(); |
| 106 assertEquals(expect, RegExp[property]); |
| 107 } |
| 108 |
| 109 OverrideCase(function() { return input.replace(re, "x"); }); |
| 110 OverrideCase(function() { return input.replace(re_g, "x"); }); |
| 111 OverrideCase(function() { return input.replace(re, ""); }); |
| 112 OverrideCase(function() { return input.replace(re_g, ""); }); |
| 113 OverrideCase(function() { return input.match(re); }); |
| 114 OverrideCase(function() { return input.match(re_g); }); |
| 115 OverrideCase(function() { return re.test(input); }); |
| 116 OverrideCase(function() { return re_g.test(input); }); |
| 117 } |
| 118 |
| 119 var input = "bar.foo baz......"; |
| 120 var re_str = "(ba.).*?f"; |
| 121 TestOverride(input, "bar", "$1", re_str); |
| 122 |
| 123 input = "foo bar baz"; |
| 124 var re_str = "bar"; |
| 125 TestOverride(input, "bar", "$&", re_str); |
| 126 |
| 127 |
| 128 function no_last_match(fn) { |
| 129 fn(); |
| 130 assertEquals("hestfisk", RegExp.$1); |
| 131 } |
| 132 |
| 133 /(hestfisk)/.test("There's no such thing as a hestfisk!"); |
| 134 |
| 135 no_last_match(function() { "foo".replace("f", ""); }); |
| 136 no_last_match(function() { "foo".replace("f", "f"); }); |
| 137 no_last_match(function() { "foo".split("o"); }); |
| 138 |
| 139 var base = "In the music. In the music. "; |
| 140 var cons = base + base + base + base; |
| 141 no_last_match(function() { cons.replace("x", "y"); }); |
| 142 no_last_match(function() { cons.replace("e", "E"); }); |
| 143 |
| 144 |
| 145 // Here's one that matches once, then tries to match again, but fails. |
| 146 // Verify that the last match info is from the last match, not from the |
| 147 // failure that came after. |
| 148 "bar.foo baz......".replace(/(ba.).*?f/g, function() { return "x";}); |
| 149 assertEquals("bar", RegExp.$1); |
| 150 |
| 151 |
| 152 var a = "foo bar baz".replace(/^|bar/g, ""); |
| 153 assertEquals("foo baz", a); |
| 154 |
| 155 a = "foo bar baz".replace(/^|bar/g, "*"); |
| 156 assertEquals("*foo * baz", a); |
OLD | NEW |