RegExp

Functions for handling RegExp's in ReScript.

See RegExp on MDN.

escape

RESCRIPT
let escape: string => string

escape(string) escapes any potential regex syntax characters in a string.

See RegExp.escape on MDN.

Examples

RESCRIPT
let 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.

exec

RESCRIPT
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" }

flags

RESCRIPT
let flags: t => string

flags(regexp) returns a string consisting of all the flags set on this RegExp.

See RegExp.flags on MDN.

Examples

RESCRIPT
let regexp = RegExp.fromString("\\w+", ~flags="gi") Console.log(regexp->RegExp.flags) // Logs "gi", all the flags set on the RegExp

fromString

RESCRIPT
let fromString: (string, ~flags: string=?) => t

fromString(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

RESCRIPT
let fromStringWithFlags: (string, ~flags: string) => t

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.

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" }

global

RESCRIPT
let global: t => bool

global(regexp) returns whether the global (g) flag is set on this RegExp.

See RegExp.global on MDN.

Examples

RESCRIPT
let 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

ignore

RESCRIPT
let ignore: t => unit

ignore(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.

ignoreCase

RESCRIPT
let ignoreCase: t => bool

ignoreCase(regexp) returns whether the ignore case (i) flag is set on this RegExp.

See RegExp.ignoreCase on MDN.

Examples

RESCRIPT
let 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

lastIndex

RESCRIPT
let lastIndex: t => int

lastIndex(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

multiline

RESCRIPT
let multiline: t => bool

multiline(regexp) returns whether the multiline (m) flag is set on this RegExp.

See RegExp.multiline on MDN.

Examples

RESCRIPT
let 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

setLastIndex

RESCRIPT
let setLastIndex: (t, int) => unit

setLastIndex(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

source

RESCRIPT
let source: t => string

source(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

RESCRIPT
let regexp = RegExp.fromStringWithFlags("\\w+", ~flags="g") Console.log(regexp->RegExp.source) // Logs `\w+`, the source text of the `RegExp`

sticky

RESCRIPT
let sticky: t => bool

sticky(regexp) returns whether the sticky (y) flag is set on this RegExp.

See RegExp.sticky on MDN.

Examples

RESCRIPT
let 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

t

RESCRIPT
type t

Type representing an instantiated RegExp.

test

RESCRIPT
let test: (t, string) => bool

test(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.") }

unicode

RESCRIPT
let unicode: t => bool

unicode(regexp) returns whether the unicode (y) flag is set on this RegExp.

See RegExp.unicode on MDN.

Examples

RESCRIPT
let 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