|
8 | 8 | import static org.junit.Assert.assertTrue;
|
9 | 9 | import static org.junit.Assert.fail;
|
10 | 10 |
|
| 11 | +import java.util.function.BiFunction; |
11 | 12 | import org.junit.Test;
|
12 | 13 | import org.mozilla.javascript.Context;
|
13 | 14 | import org.mozilla.javascript.ScriptableObject;
|
@@ -700,4 +701,35 @@ public void backwardFlatCaseInsensitiveNoMatch() throws Exception {
|
700 | 701 | final String script = "var regex = /abc(?<=XYZ)/i;\n" + "'abc'.match(regex);";
|
701 | 702 | Utils.assertWithAllModes_ES6(null, script);
|
702 | 703 | }
|
| 704 | + |
| 705 | + @Test |
| 706 | + public void quantifiedCaptureClearsPreviousCaptures() { |
| 707 | + // With the pattern /(?:(\2)(\d))*/, the first capture group should be always empty |
| 708 | + // when used with a quantifier |
| 709 | + BiFunction<String, String, String> test = |
| 710 | + (quantifier, input) -> { |
| 711 | + return "var regexStr = '(?:(\\\\2)(\\\\d))'\n" |
| 712 | + + "function test(quantifier, input) {\n" |
| 713 | + + " var regexp = new RegExp(regexStr + quantifier + '$');\n" |
| 714 | + + " var res = regexp.exec(input);\n" |
| 715 | + + " return res != null && res.length == 3 && res[1] == '' && res[2] != '';\n" |
| 716 | + + "}\n" |
| 717 | + + "test('" |
| 718 | + + quantifier |
| 719 | + + "','" |
| 720 | + + input |
| 721 | + + "');"; |
| 722 | + }; |
| 723 | + |
| 724 | + Utils.assertWithAllModes_ES6("greedy-*", true, test.apply("*", "123")); |
| 725 | + Utils.assertWithAllModes_ES6("greedy-+", true, test.apply("+", "123")); |
| 726 | + Utils.assertWithAllModes_ES6("greedy-?", true, test.apply("?", "123")); |
| 727 | + Utils.assertWithAllModes_ES6("greedy-{2}", true, test.apply("{2}", "123")); |
| 728 | + Utils.assertWithAllModes_ES6("greedy-{2,3}", true, test.apply("{2,}", "123")); |
| 729 | + Utils.assertWithAllModes_ES6("non-greedy-*", true, test.apply("*?", "123")); |
| 730 | + Utils.assertWithAllModes_ES6("non-greedy-+", true, test.apply("+?", "123")); |
| 731 | + Utils.assertWithAllModes_ES6("non-greedy-?", true, test.apply("??", "123")); |
| 732 | + Utils.assertWithAllModes_ES6("non-greedy-{2}", true, test.apply("{2}?", "123")); |
| 733 | + Utils.assertWithAllModes_ES6("non-greedy-{2,3}", true, test.apply("{2,}?", "123")); |
| 734 | + } |
703 | 735 | }
|
0 commit comments