RegExp
Functions for handling RegExp's in ReScript.
See RegExp
on MDN.
t
type t = Js.Re.t
Type representing an instantiated RegExp
.
fromString
let fromString: 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"
}
fromStringWithFlags
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"
}
test
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.")
}
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 => 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
setLastIndex
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
ignoreCase
let ignoreCase: t => bool
ignoreCase(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 => bool
global(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 => bool
multiline(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 => 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
RESCRIPTlet regexp = RegExp.fromStringWithFlags("\\w+", ~flags="g")
Console.log(regexp->RegExp.source) // Logs `\w+`, the source text of the `RegExp`
sticky
let sticky: t => bool
sticky(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 => bool
unicode(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