1 The `regexp()` function compiles the given pattern string into a regular 2 expression, optionally applying the flags specified in the second argument. 3 4 Throws an exception if unrecognized flag characters are specified or if the 5 flags argument is not a string value. 6 7 Throws an exception if the given pattern string cannot be compiled into a 8 regular expression. 9 10 Returns the compiled regexp object. 11 12 -- Testcase -- 13 {% 14 let re1 = regexp("begin (.+) end", "i"); 15 let re2 = regexp("[a-z]+", "g"); 16 let re3 = regexp("Dots (.+) newlines", "s"); 17 18 printf("%.J\n", [ 19 match("BEGIN this is some text END", re1), 20 match("This is a group of words", re2), 21 match("Dots now\ndon't\nmatch\ntext\nwith newlines", re3) 22 ]); 23 %} 24 -- End -- 25 26 -- Expect stdout -- 27 [ 28 [ 29 "BEGIN this is some text END", 30 "this is some text" 31 ], 32 [ 33 [ 34 "his" 35 ], 36 [ 37 "is" 38 ], 39 [ 40 "a" 41 ], 42 [ 43 "group" 44 ], 45 [ 46 "of" 47 ], 48 [ 49 "words" 50 ] 51 ], 52 null 53 ] 54 -- End -- 55 56 57 Passing an uncompilable regexp throws an exception. 58 59 -- Testcase -- 60 {% 61 try { 62 // unterminated capture group to trigger syntax error 63 regexp("foo("); 64 } 65 catch (e) { 66 // Massage compile error message for stable output since it is 67 // dependant on the underyling C library. 68 e.message = "Compile error"; 69 die(e); 70 } 71 %} 72 -- End -- 73 74 -- Expect stderr -- 75 Compile error 76 In line 10, byte 8: 77 78 ` die(e);` 79 Near here ---^ 80 81 82 -- End -- 83 84 85 Passing an invalid flags argument throws an exception. 86 87 -- Testcase -- 88 {% 89 regexp(".*", true); 90 %} 91 -- End -- 92 93 -- Expect stderr -- 94 Type error: Given flags argument is not a string 95 In line 2, byte 19: 96 97 ` regexp(".*", true);` 98 Near here -----------^ 99 100 101 -- End -- 102 103 -- Testcase -- 104 {% 105 regexp(".*", "igz"); 106 %} 107 -- End -- 108 109 -- Expect stderr -- 110 Type error: Unrecognized flag character 'z' 111 In line 2, byte 20: 112 113 ` regexp(".*", "igz");` 114 Near here ------------^ 115 116 117 -- End --
This page was automatically generated by LXR 0.3.1. • OpenWrt