RegExp
Functions for handling RegExp's in ReScript.
See RegExp on MDN.
t
type tType representing an instantiated RegExp.
fromString
let fromString: (string, ~flags: string=?) => tfromString(string) creates a RegExp.t from the provided string. This can then be used to match on strings using RegExp.exec.
See RegExp on MDN.
Examples
RESCRIPT// Match the first word in a sentence
let regexp = RegExp.fromString("\\w+")
switch regexp->RegExp.exec("ReScript is pretty cool, right?") {
| None => Console.log("Nope, no match...")
| Some(result) => Console.log(result->RegExp.Result.fullMatch) // Prints "ReScript"
}
// Match 'foo' with case insensitive flag
let regexp = RegExp.fromString("foo", ~flags="i")
switch regexp->RegExp.exec("FOO") {
| None => Console.log("Nope, no match...")
| Some(result) => Console.log(result->RegExp.Result.fullMatch) // Prints "FOO"
}
fromStringWithFlags
Deprecated
let fromStringWithFlags: (string, ~flags: string) => tfromStringWithFlags(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.
See RegExp parameters on MDN.
Examples
RESCRIPT// Match the first word in a sentence
let regexp = RegExp.fromStringWithFlags("\\w+", ~flags="g")
switch regexp->RegExp.exec("ReScript is pretty cool, right?") {
| None => Console.log("Nope, no match...")
| Some(result) => Console.log(result->RegExp.Result.fullMatch) // Prints "ReScript"
}
escape
let escape: string => stringescape(string) escapes any potential regex syntax characters in a string.
See RegExp.escape on MDN.
Examples
RESCRIPTlet literal = "foo[bar]"
let regexp = literal->RegExp.escape->RegExp.fromString
regexp->RegExp.test("foo[bar]") == true
Remark
Since May 2025, this feature works across the latest devices and browser versions. This feature might not work in older devices or browsers.
test
let test: (t, string) => booltest(regexp, string) tests whether the provided regexp matches on the provided string.
See RegExp.test on MDN.
Examples
RESCRIPT// Match the first word in a sentence
let regexp = RegExp.fromString("\\w+")
if regexp->RegExp.test("ReScript is cool!") {
Console.log("Yay, there's a word in there.")
}
exec
let exec: (t, string) => option<Result.t>exec(regexp, string) executes the provided regexp on the provided string, optionally returning a RegExp.Result.t if the regexp matches on the string.
See RegExp.exec on MDN.
Examples
RESCRIPT// Match the first word in a sentence
let regexp = RegExp.fromString("\\w+")
switch regexp->RegExp.exec("ReScript is pretty cool, right?") {
| None => Console.log("Nope, no match...")
| Some(result) => Console.log(result->RegExp.Result.fullMatch) // Prints "ReScript"
}
lastIndex
let lastIndex: t => intlastIndex(regexp) returns the index the next match will start from.
See RegExp.lastIndex on MDN.
Examples
RESCRIPT// Match the first word in a sentence
let regexp = RegExp.fromString("\\w+")
let someStr = "Many words here."
Console.log(regexp->RegExp.lastIndex) // Logs `0` to the console
regexp->RegExp.exec(someStr)->ignore
Console.log(regexp->RegExp.lastIndex) // Logs `4` to the console
setLastIndex
let setLastIndex: (t, int) => unitsetLastIndex(regexp, index) set the index the next match will start from.
See RegExp.lastIndex on MDN.
Examples
RESCRIPT// Match the first word in a sentence
let regexp = RegExp.fromString("\\w+")
let someStr = "Many words here."
regexp->RegExp.setLastIndex(4)
regexp->RegExp.exec(someStr)->ignore
Console.log(regexp->RegExp.lastIndex) // Logs `10` to the console
ignoreCase
let ignoreCase: t => boolignoreCase(regexp) returns whether the ignore case (i) flag is set on this RegExp.
See RegExp.ignoreCase on MDN.
Examples
RESCRIPTlet regexp1 = RegExp.fromStringWithFlags("\\w+", ~flags="g")
Console.log(regexp1->RegExp.ignoreCase) // Logs `false`, since `i` is not set
let regexp2 = RegExp.fromStringWithFlags("\\w+", ~flags="i")
Console.log(regexp2->RegExp.ignoreCase) // Logs `true`, since `i` is set
global
let global: t => boolglobal(regexp) returns whether the global (g) flag is set on this RegExp.
See RegExp.global on MDN.
Examples
RESCRIPTlet regexp1 = RegExp.fromStringWithFlags("\\w+", ~flags="g")
Console.log(regexp1->RegExp.global) // Logs `true`, since `g` is set
let regexp2 = RegExp.fromStringWithFlags("\\w+", ~flags="i")
Console.log(regexp2->RegExp.global) // Logs `false`, since `g` is not set
multiline
let multiline: t => boolmultiline(regexp) returns whether the multiline (m) flag is set on this RegExp.
See RegExp.multiline on MDN.
Examples
RESCRIPTlet regexp1 = RegExp.fromStringWithFlags("\\w+", ~flags="g")
Console.log(regexp1->RegExp.multiline) // Logs `false`, since `m` is not set
let regexp2 = RegExp.fromStringWithFlags("\\w+", ~flags="mi")
Console.log(regexp2->RegExp.multiline) // Logs `true`, since `m` is set
source
let source: t => stringsource(regexp) returns the source text for this RegExp, without the two forward slashes (if present), and without any set flags.
See RegExp.source on MDN.
Examples
RESCRIPTlet regexp = RegExp.fromStringWithFlags("\\w+", ~flags="g")
Console.log(regexp->RegExp.source) // Logs `\w+`, the source text of the `RegExp`
sticky
let sticky: t => boolsticky(regexp) returns whether the sticky (y) flag is set on this RegExp.
See RegExp.sticky on MDN.
Examples
RESCRIPTlet regexp1 = RegExp.fromStringWithFlags("\\w+", ~flags="g")
Console.log(regexp1->RegExp.unicode) // Logs `false`, since `y` is not set
let regexp2 = RegExp.fromStringWithFlags("\\w+", ~flags="my")
Console.log(regexp2->RegExp.unicode) // Logs `true`, since `y` is set
unicode
let unicode: t => boolunicode(regexp) returns whether the unicode (y) flag is set on this RegExp.
See RegExp.unicode on MDN.
Examples
RESCRIPTlet regexp1 = RegExp.fromStringWithFlags("\\w+", ~flags="g")
Console.log(regexp1->RegExp.unicode) // Logs `false`, since `u` is not set
let regexp2 = RegExp.fromStringWithFlags("\\w+", ~flags="mu")
Console.log(regexp2->RegExp.unicode) // Logs `true`, since `u` is set
flags
let flags: t => stringflags(regexp) returns a string consisting of all the flags set on this RegExp.
See RegExp.flags on MDN.
Examples
RESCRIPTlet regexp = RegExp.fromString("\\w+", ~flags="gi")
Console.log(regexp->RegExp.flags) // Logs "gi", all the flags set on the RegExp
ignore
let ignore: t => unitignore(regExp) ignores the provided regExp and returns unit.
This helper is useful when you want to discard a value (for example, the result of an operation with side effects) without having to store or process it further.