|
| 1 | +/*** |
| 2 | +Functions for handling RegExp's in ReScript. |
| 3 | + |
| 4 | +See [`RegExp`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp) on MDN. |
| 5 | +*/ |
| 6 | + |
| 7 | +/** |
| 8 | +Type representing an instantiated `RegExp`. |
| 9 | +*/ |
| 10 | +type t = Js.Re.t |
| 11 | + |
| 12 | +module Result: { |
| 13 | + /** |
| 14 | + Type representing the result of a `RegExp` execution. |
| 15 | + */ |
| 16 | + type t = array<string> |
| 17 | + |
| 18 | + /** |
| 19 | + `fullMatch(regExpResult)` returns the full string that matched in this result. |
| 20 | + |
| 21 | + ## Examples |
| 22 | + ```rescript |
| 23 | + // Match the first two words separated by a space |
| 24 | + let regexp = RegExp.fromString("(\\w+) (\\w+)") |
| 25 | + |
| 26 | + switch regexp->RegExp.exec("ReScript is pretty cool, right?") { |
| 27 | + | None => Console.log("Nope, no match...") |
| 28 | + | Some(result) => Console.log(result->RegExp.Result.fullMatch) // Prints the full string that matched, "ReScript is" |
| 29 | + } |
| 30 | + ``` |
| 31 | + */ |
| 32 | + @get_index |
| 33 | + external fullMatch: (t, @as(0) _) => string = "" |
| 34 | + |
| 35 | + /** |
| 36 | + `matches(regExpResult)` returns all matches for `regExpResult`. |
| 37 | + |
| 38 | + ## Examples |
| 39 | + ```rescript |
| 40 | + // Match the first two words separated by a space |
| 41 | + let regexp = RegExp.fromString("(\\w+) (\\w+)") |
| 42 | + |
| 43 | + // This below will log "ReScript" and "is" to the console. |
| 44 | + switch regexp->RegExp.exec("ReScript is pretty cool, right?") { |
| 45 | + | None => Console.log("Nope, no match...") |
| 46 | + | Some(result) => switch result->RegExp.Result.matches { |
| 47 | + | [firstWord, secondWord] => Console.log2(firstWord, secondWord) |
| 48 | + | _ => Console.log("Didn't find exactly two words...") |
| 49 | + } |
| 50 | + } |
| 51 | + ``` |
| 52 | + */ |
| 53 | + @send |
| 54 | + external matches: (t, @as(1) _) => array<string> = "slice" |
| 55 | + @get external index: t => int = "index" |
| 56 | + |
| 57 | + /** |
| 58 | + `input(regExpResult)` returns the full input string that was passed to what produced the `RegExp.Result.t`. |
| 59 | + |
| 60 | + ## Examples |
| 61 | + ```rescript |
| 62 | + // Match the first two words separated by a space |
| 63 | + let regexp = RegExp.fromString("(\\w+) (\\w+)") |
| 64 | + |
| 65 | + // This below will log the full input string "ReScript is pretty cool, right?" to the console. |
| 66 | + switch regexp->RegExp.exec("ReScript is pretty cool, right?") { |
| 67 | + | None => Console.log("Nope, no match...") |
| 68 | + | Some(result) => Console.log(result->RegExp.Result.input) |
| 69 | + } |
| 70 | + ``` |
| 71 | + */ |
| 72 | + @get |
| 73 | + external input: t => string = "input" |
| 74 | +} |
| 75 | + |
| 76 | +/** |
| 77 | +`fromString(string)` creates a `RegExp.t` from the provided string. This can then be used to match on strings using `RegExp.exec`. |
| 78 | + |
| 79 | +See [`RegExp`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/RegExp) on MDN. |
| 80 | + |
| 81 | +## Examples |
| 82 | +```rescript |
| 83 | +// Match the first word in a sentence |
| 84 | +let regexp = RegExp.fromString("\\w+") |
| 85 | + |
| 86 | +switch regexp->RegExp.exec("ReScript is pretty cool, right?") { |
| 87 | +| None => Console.log("Nope, no match...") |
| 88 | +| Some(result) => Console.log(result->RegExp.Result.fullMatch) // Prints "ReScript" |
| 89 | +} |
| 90 | +``` |
| 91 | +*/ |
| 92 | +@new |
| 93 | +external fromString: string => t = "RegExp" |
| 94 | + |
| 95 | +/** |
| 96 | +`fromStringWithFlags(string)` creates a `RegExp.t` from the provided string, using the provided `flags`. This can then be used to match on strings using `RegExp.exec`. |
| 97 | + |
| 98 | +See [`RegExp parameters`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/RegExp#parameters) on MDN. |
| 99 | + |
| 100 | +## Examples |
| 101 | +```rescript |
| 102 | +// Match the first word in a sentence |
| 103 | +let regexp = RegExp.fromStringWithFlags("\\w+", ~flags="g") |
| 104 | + |
| 105 | +switch regexp->RegExp.exec("ReScript is pretty cool, right?") { |
| 106 | +| None => Console.log("Nope, no match...") |
| 107 | +| Some(result) => Console.log(result->RegExp.Result.fullMatch) // Prints "ReScript" |
| 108 | +} |
| 109 | +``` |
| 110 | +*/ |
| 111 | +@new |
| 112 | +external fromStringWithFlags: (string, ~flags: string) => t = "RegExp" |
| 113 | + |
| 114 | +/** |
| 115 | +`test(regexp, string)` tests whether the provided `regexp` matches on the provided string. |
| 116 | + |
| 117 | +See [`RegExp.test`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/test) on MDN. |
| 118 | + |
| 119 | +## Examples |
| 120 | +```rescript |
| 121 | +// Match the first word in a sentence |
| 122 | +let regexp = RegExp.fromString("\\w+") |
| 123 | + |
| 124 | +if regexp->RegExp.test("ReScript is cool!") { |
| 125 | + Console.log("Yay, there's a word in there.") |
| 126 | +} |
| 127 | +``` |
| 128 | +*/ |
| 129 | +@send |
| 130 | +external test: (t, string) => bool = "test" |
| 131 | + |
| 132 | +/** |
| 133 | +`exec(regexp, string)` executes the provided regexp on the provided string, optionally returning a `RegExp.Result.t` if the regexp matches on the string. |
| 134 | + |
| 135 | +See [`RegExp.exec`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/exec) on MDN. |
| 136 | + |
| 137 | +## Examples |
| 138 | +```rescript |
| 139 | +// Match the first word in a sentence |
| 140 | +let regexp = RegExp.fromString("\\w+") |
| 141 | + |
| 142 | +switch regexp->RegExp.exec("ReScript is pretty cool, right?") { |
| 143 | +| None => Console.log("Nope, no match...") |
| 144 | +| Some(result) => Console.log(result->RegExp.Result.fullMatch) // Prints "ReScript" |
| 145 | +} |
| 146 | +``` |
| 147 | +*/ |
| 148 | +@return(nullable) |
| 149 | +@send |
| 150 | +external exec: (t, string) => option<Result.t> = "exec" |
| 151 | + |
| 152 | +/** |
| 153 | +`lastIndex(regexp)` returns the index the next match will start from. |
| 154 | + |
| 155 | +See [`RegExp.lastIndex`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/lastIndex) on MDN. |
| 156 | + |
| 157 | +## Examples |
| 158 | +```rescript |
| 159 | +// Match the first word in a sentence |
| 160 | +let regexp = RegExp.fromString("\\w+") |
| 161 | +let someStr = "Many words here." |
| 162 | + |
| 163 | +Console.log(regexp->RegExp.lastIndex) // Logs `0` to the console |
| 164 | + |
| 165 | +regexp->RegExp.exec(someStr)->ignore |
| 166 | + |
| 167 | +Console.log(regexp->RegExp.lastIndex) // Logs `4` to the console |
| 168 | +``` |
| 169 | +*/ |
| 170 | +@get |
| 171 | +external lastIndex: t => int = "lastIndex" |
| 172 | + |
| 173 | +/** |
| 174 | +`ignoreCase(regexp)` returns whether the ignore case (`i`) flag is set on this `RegExp`. |
| 175 | + |
| 176 | +See [`RegExp.ignoreCase`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/ignoreCase) on MDN. |
| 177 | + |
| 178 | +## Examples |
| 179 | +```rescript |
| 180 | +let regexp1 = RegExp.fromStringWithFlags("\\w+", ~flags="g") |
| 181 | +Console.log(regexp1->RegExp.ignoreCase) // Logs `false`, since `i` is not set |
| 182 | + |
| 183 | +let regexp2 = RegExp.fromStringWithFlags("\\w+", ~flags="i") |
| 184 | +Console.log(regexp2->RegExp.ignoreCase) // Logs `true`, since `i` is set |
| 185 | +``` |
| 186 | +*/ |
| 187 | +@get |
| 188 | +external ignoreCase: t => bool = "ignoreCase" |
| 189 | + |
| 190 | +/** |
| 191 | +`global(regexp)` returns whether the global (`g`) flag is set on this `RegExp`. |
| 192 | + |
| 193 | +See [`RegExp.global`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/global) on MDN. |
| 194 | + |
| 195 | +## Examples |
| 196 | +```rescript |
| 197 | +let regexp1 = RegExp.fromStringWithFlags("\\w+", ~flags="g") |
| 198 | +Console.log(regexp1->RegExp.global) // Logs `true`, since `g` is set |
| 199 | + |
| 200 | +let regexp2 = RegExp.fromStringWithFlags("\\w+", ~flags="i") |
| 201 | +Console.log(regexp2->RegExp.global) // Logs `false`, since `g` is not set |
| 202 | +``` |
| 203 | +*/ |
| 204 | +@get |
| 205 | +external global: t => bool = "global" |
| 206 | + |
| 207 | +/** |
| 208 | +`multiline(regexp)` returns whether the multiline (`m`) flag is set on this `RegExp`. |
| 209 | + |
| 210 | +See [`RegExp.multiline`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/multiline) on MDN. |
| 211 | + |
| 212 | +## Examples |
| 213 | +```rescript |
| 214 | +let regexp1 = RegExp.fromStringWithFlags("\\w+", ~flags="g") |
| 215 | +Console.log(regexp1->RegExp.multiline) // Logs `false`, since `m` is not set |
| 216 | + |
| 217 | +let regexp2 = RegExp.fromStringWithFlags("\\w+", ~flags="mi") |
| 218 | +Console.log(regexp2->RegExp.multiline) // Logs `true`, since `m` is set |
| 219 | +``` |
| 220 | +*/ |
| 221 | +@get |
| 222 | +external multiline: t => bool = "multiline" |
| 223 | + |
| 224 | +/** |
| 225 | +`source(regexp)` returns the source text for this `RegExp`, without the two forward slashes (if present), and without any set flags. |
| 226 | + |
| 227 | +See [`RegExp.source`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/source) on MDN. |
| 228 | + |
| 229 | +## Examples |
| 230 | +```rescript |
| 231 | +let regexp = RegExp.fromStringWithFlags("\\w+", ~flags="g") |
| 232 | +Console.log(regexp->RegExp.source) // Logs `\w+`, the source text of the `RegExp` |
| 233 | +``` |
| 234 | +*/ |
| 235 | +@get |
| 236 | +external source: t => string = "source" |
| 237 | + |
| 238 | +/** |
| 239 | +`sticky(regexp)` returns whether the sticky (`y`) flag is set on this `RegExp`. |
| 240 | + |
| 241 | +See [`RegExp.sticky`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/sticky) on MDN. |
| 242 | + |
| 243 | +## Examples |
| 244 | +```rescript |
| 245 | +let regexp1 = RegExp.fromStringWithFlags("\\w+", ~flags="g") |
| 246 | +Console.log(regexp1->RegExp.unicode) // Logs `false`, since `y` is not set |
| 247 | + |
| 248 | +let regexp2 = RegExp.fromStringWithFlags("\\w+", ~flags="my") |
| 249 | +Console.log(regexp2->RegExp.unicode) // Logs `true`, since `y` is set |
| 250 | +``` |
| 251 | +*/ |
| 252 | +@get |
| 253 | +external sticky: t => bool = "sticky" |
| 254 | + |
| 255 | +/** |
| 256 | +`unicode(regexp)` returns whether the unicode (`y`) flag is set on this `RegExp`. |
| 257 | + |
| 258 | +See [`RegExp.unicode`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/unicode) on MDN. |
| 259 | + |
| 260 | +## Examples |
| 261 | +```rescript |
| 262 | +let regexp1 = RegExp.fromStringWithFlags("\\w+", ~flags="g") |
| 263 | +Console.log(regexp1->RegExp.unicode) // Logs `false`, since `u` is not set |
| 264 | + |
| 265 | +let regexp2 = RegExp.fromStringWithFlags("\\w+", ~flags="mu") |
| 266 | +Console.log(regexp2->RegExp.unicode) // Logs `true`, since `u` is set |
| 267 | +``` |
| 268 | +*/ |
| 269 | +@get |
| 270 | +external unicode: t => bool = "unicode" |
0 commit comments