bug593663-regexp.js (3220B)
1 /* 2 * Ensure that flat matches with metachars in them don't have their metachars 3 * interpreted as special. 4 */ 5 6 function isPatternSyntaxError(pattern) { 7 try { 8 new RegExp(pattern); 9 return false; 10 } catch (e) { 11 if (!(e instanceof SyntaxError)) 12 throw e; 13 return true; 14 } 15 } 16 17 // Bug example. 18 assertEq("1+2".replace("1+2", "$&+3"), "1+2+3"); 19 assertEq("1112".replace("1+2", ""), "1112"); 20 21 // ^ 22 assertEq("leading text^my hat".replace("^my hat", ""), "leading text"); 23 assertEq("my hat".replace("^my hat", ""), "my hat"); 24 25 // $ 26 assertEq("leading text$my money".replace("leading text$", ""), "my money"); 27 assertEq("leading text".replace("leading text$", ""), "leading text"); 28 29 // \ 30 var BSL = '\\'; 31 assertEq(("dir C:" + BSL + "Windoze").replace("C:" + BSL, ""), 32 "dir Windoze"); 33 assertEq(isPatternSyntaxError("C:" + BSL), true); 34 35 // . 36 assertEq("This is a sentence. It has words.".replace(".", "!"), 37 "This is a sentence! It has words."); 38 assertEq("This is an unterminated sentence".replace(".", ""), 39 "This is an unterminated sentence"); 40 41 // * 42 assertEq("Video killed the radio *".replace(" *", ""), "Video killed the radio"); 43 assertEq("aaa".replace("a*", ""), "aaa"); 44 45 // + 46 assertEq("On the + side".replace(" +", ""), "On the side"); 47 assertEq("1111111111111".replace("1+", ""), "1111111111111"); 48 49 // \+ 50 assertEq(("Inverse cone head: " + BSL + "++/").replace(BSL + "+", ""), 51 "Inverse cone head: +/"); 52 assertEq((BSL + BSL + BSL).replace(BSL + "+", ""), 53 BSL + BSL + BSL); 54 55 // \\+ 56 assertEq((BSL + BSL + "+").replace(BSL + BSL + "+", ""), ""); 57 assertEq((BSL + BSL + BSL).replace(BSL + BSL + "+", ""), (BSL + BSL + BSL)); 58 59 // \\\ 60 assertEq((BSL + BSL + BSL + BSL).replace(BSL + BSL + BSL, ""), BSL); 61 assertEq(isPatternSyntaxError(BSL + BSL + BSL), true); 62 63 // \\\\ 64 assertEq((BSL + BSL + BSL + BSL).replace(BSL + BSL + BSL + BSL, "", "i"), ""); 65 assertEq((BSL + BSL).replace(BSL + BSL + BSL + BSL, ""), BSL + BSL); 66 67 68 // ? 69 assertEq("Pressing question?".replace("?", "."), "Pressing question."); 70 assertEq("a".replace("a?", ""), "a"); 71 72 // ( 73 assertEq("(a".replace("(", ""), "a"); 74 75 // ) 76 assertEq("a)".replace(")", ""), "a"); 77 78 // ( and ) 79 assertEq("(foo)".replace("(foo)", ""), ""); 80 assertEq("a".replace("(a)", ""), "a"); 81 82 // [ 83 assertEq("[a".replace("[", ""), "a"); 84 85 // ] 86 assertEq("a]".replace("]", ""), "a"); 87 88 // [ and ] 89 assertEq("a".replace("[a-z]", ""), "a"); 90 assertEq("You would write your regexp as [a-z]".replace("[a-z]", ""), 91 "You would write your regexp as "); 92 93 // { 94 assertEq("Numbers may be specified in the interval {1,100}".replace("{1,", ""), 95 "Numbers may be specified in the interval 100}"); 96 97 // } 98 assertEq("Numbers may be specified in the interval {1,100}".replace(",100}", ""), 99 "Numbers may be specified in the interval {1"); 100 101 // { and } 102 assertEq("Numbers may be specified in the interval {1,100}".replace(" {1,100}", ""), 103 "Numbers may be specified in the interval"); 104 assertEq("aaa".replace("a{1,10}", ""), "aaa"); 105 106 // | 107 assertEq("Mr. Gorbachev|Tear down this wall!".replace("|Tear down this wall!", ""), 108 "Mr. Gorbachev"); 109 assertEq("foobar".replace("foo|bar", ""), "foobar"); 110 111 print("PASS");