1 The `replace()` function replaces the given regular expression or plain 2 string pattern on the given subject value with the specified replacement. 3 4 In case a regular expression with the global (`g`) modifier set or a 5 string is passed as pattern, all found occurrences are replaced. In case 6 a regular expression without global modifier is given, only the first 7 match will be replaced. 8 9 The replacement value may be either a string, which is inserted in place 10 of the matched result after certain interpolation steps or a function 11 which is invoked for each match and whose return value is used as 12 replacement. 13 14 The subject is implicitly converted to a string if it is not a string. 15 16 The pattern is implicitly converted to a string if it is neither a string 17 nor a regular expression value. 18 19 The replacement value is implicitly converted to a string if it is neither 20 a string nor a function value. 21 22 Returns a copy of the input string with the match(es) replaced by their 23 corresponding replacement values. 24 25 Returns `null` either the subject, the pattern or the replacement value 26 is `null`. 27 28 -- Testcase -- 29 {% 30 print(join("\n###\n", [ 31 // Capitalize and reformat all key=value pairs using a callback 32 replace("kind=fruit name=strawberry color=red", 33 /([[:alpha:]])([[:alpha:]]*)=(.)([^= ]*) */g, 34 function(m, letter1, rest1, letter2, rest2) { 35 return sprintf('%s%s: %s%s\n', 36 uc(letter1), rest1, 37 uc(letter2), rest2 38 ); 39 }), 40 41 // strike any three letter word 42 replace("The quick brown fox jumps over the lazy dog", 43 /(^| )([[:alpha:]]{3})( |$)/g, 44 "$1<s>$2</s>$3"), 45 46 // highlight any vowel 47 replace("The quick brown fox jumps over the lazy dog", 48 /[aeiou]/g, 49 "[$&]"), 50 51 // replace with fixed pattern 52 replace("foo bar foo baz foo qrx", "foo", "xxx"), 53 54 // testing all possible replacement interpolations 55 replace("before abc def ghi jkl mno pqr stu vwx yz! after", 56 / ([a-z]{3}) ([a-z]{3}) ([a-z]{3}) ([a-z]{3}) ([a-z]{3}) ([a-z]{3}) ([a-z]{3}) ([a-z]{3}) ([a-z!]{3}) /, 57 '|\n---\n' + 58 'Entire match ($$&): [$&]\n' + 59 'Before match ($$`): [$`]\n' + 60 "After match ($$'): [$']\n" + 61 'Group 1 match ($$1): [$1]\n' + 62 'Group 2 match ($$2): [$2]\n' + 63 'Group 3 match ($$3): [$3]\n' + 64 'Group 4 match ($$4): [$4]\n' + 65 'Group 5 match ($$5): [$5]\n' + 66 'Group 6 match ($$6): [$6]\n' + 67 'Group 7 match ($$7): [$7]\n' + 68 'Group 8 match ($$8): [$8]\n' + 69 'Group 9 match ($$9): [$9]\n' + 70 'Literal $$: [$$]\n' + 71 '---\n|'), 72 73 // testing that all captures are passed to the callback 74 replace("before abc def ghi jkl mno pqr stu vwx yz! after", 75 / ([a-z]{3}) ([a-z]{3}) ([a-z]{3}) ([a-z]{3}) ([a-z]{3}) ([a-z]{3}) ([a-z]{3}) ([a-z]{3}) ([a-z!]{3}) /, 76 function(m0, m1, m2, m3, m4, m5, m6, m7, m8, m9) { 77 return sprintf( 78 '|\n---\n' + 79 'Entire match (arg 0): [%s]\n' + 80 'Group 1 match (arg 1): [%s]\n' + 81 'Group 2 match (arg 2): [%s]\n' + 82 'Group 3 match (arg 3): [%s]\n' + 83 'Group 4 match (arg 4): [%s]\n' + 84 'Group 5 match (arg 5): [%s]\n' + 85 'Group 6 match (arg 6): [%s]\n' + 86 'Group 7 match (arg 7): [%s]\n' + 87 'Group 8 match (arg 8): [%s]\n' + 88 'Group 9 match (arg 9): [%s]\n' + 89 '---\n|', 90 m0, m1, m2, m3, m4, m5, m6, m7, m8, m9 91 ); 92 }), 93 94 // the subject is implictly stringified 95 replace({ foo: true }, "foo", "xxx"), 96 97 // the pattern is implictly stringified 98 replace({ foo: true }, true, "false"), 99 100 // the replacement is implictly stringified 101 replace({ foo: true }, "foo", 0x7b), 102 103 // special case: replace all empty matches 104 replace("foo", "", "."), 105 replace("foo", /()/g, ".") 106 ]), "\n"); 107 %} 108 -- End -- 109 110 -- Expect stdout -- 111 Kind: Fruit 112 Name: Strawberry 113 Color: Red 114 115 ### 116 <s>The</s> quick brown <s>fox</s> jumps over <s>the</s> lazy <s>dog</s> 117 ### 118 Th[e] q[u][i]ck br[o]wn f[o]x j[u]mps [o]v[e]r th[e] l[a]zy d[o]g 119 ### 120 xxx bar xxx baz xxx qrx 121 ### 122 before | 123 --- 124 Entire match ($&): [ abc def ghi jkl mno pqr stu vwx yz! ] 125 Before match ($`): [before ] 126 After match ($'): [ after] 127 Group 1 match ($1): [abc] 128 Group 2 match ($2): [def] 129 Group 3 match ($3): [ghi] 130 Group 4 match ($4): [jkl] 131 Group 5 match ($5): [mno] 132 Group 6 match ($6): [pqr] 133 Group 7 match ($7): [stu] 134 Group 8 match ($8): [vwx] 135 Group 9 match ($9): [yz!] 136 Literal $: [$] 137 --- 138 | after 139 ### 140 before | 141 --- 142 Entire match (arg 0): [ abc def ghi jkl mno pqr stu vwx yz! ] 143 Group 1 match (arg 1): [abc] 144 Group 2 match (arg 2): [def] 145 Group 3 match (arg 3): [ghi] 146 Group 4 match (arg 4): [jkl] 147 Group 5 match (arg 5): [mno] 148 Group 6 match (arg 6): [pqr] 149 Group 7 match (arg 7): [stu] 150 Group 8 match (arg 8): [vwx] 151 Group 9 match (arg 9): [yz!] 152 --- 153 | after 154 ### 155 { "xxx": true } 156 ### 157 { "foo": false } 158 ### 159 { "123": true } 160 ### 161 .f.o.o. 162 ### 163 .f.o.o. 164 -- End -- 165 166 167 Omitting subject, pattern or replacement yields `null`. 168 169 -- Testcase -- 170 {% 171 printf("%.J\n", [ 172 replace(null, "u", "x"), 173 replace("nullnull", null, "x"), 174 replace("foo", "o", null) 175 ]); 176 %} 177 -- End -- 178 179 -- Expect stdout -- 180 [ 181 null, 182 null, 183 null 184 ] 185 -- End -- 186 187 188 Exceptions in the callback terminate the replacement process and are 189 propagated to the calling context. 190 191 -- Testcase -- 192 {% 193 replace("foo", "o", function(m) { die() }); 194 %} 195 -- End -- 196 197 -- Expect stderr -- 198 Died 199 In [anonymous function](), line 2, byte 40: 200 called from function replace ([C]) 201 called from anonymous function ([stdin]:2:43) 202 203 ` replace("foo", "o", function(m) { die() });` 204 Near here --------------------------------^ 205 206 207 -- End -- 208 209 210 An optional limit parameter controls the maximum amount of replacements. 211 212 -- Testcase -- 213 {% 214 printf("%.J\n", [ 215 // negative limit performs no substitution 216 replace("aaaaa", "a", "x", -1), 217 218 // zero limit performs no substitution 219 replace("aaaaa", "a", "x", 0), 220 221 // positive limit 222 replace("aaaaa", "a", "x", 3), 223 224 // same rules apply to regex replaces: 225 replace("foo bar baz", /[ao]/g, "x", -1), 226 replace("foo bar baz", /[ao]/g, "x", 0), 227 replace("foo bar baz", /[ao]/g, "x", 3), 228 ]); 229 %} 230 -- End -- 231 232 -- Expect stdout -- 233 [ 234 "aaaaa", 235 "aaaaa", 236 "xxxaa", 237 "foo bar baz", 238 "foo bar baz", 239 "fxx bxr baz" 240 ] 241 -- End --
This page was automatically generated by LXR 0.3.1. • OpenWrt