Re
Provide bindings to JS regular expressions (RegExp).
Note: This is not an immutable API. A RegExp object with the global ("g")
flag set will modify the lastIndex property when the RegExp object is used,
and subsequent uses will continue the search from the previous lastIndex.
t
Deprecated
type t = RegExp.tThe RegExp object.
result
type resultThe result of a executing a RegExp on a string.
captures
Deprecated
let captures: result => array<Js_null_undefined.t<string>>An array of the match and captures, the first is the full match and the
remaining are the substring captures.
matches
Deprecated
let matches: result => array<string>index
Deprecated
let index: result => int0-based index of the match in the input string.
input
Deprecated
let input: result => stringThe original input string.
fromString
Deprecated
let fromString: string => tConstructs a RegExp object (Js.Re.t) from a string.
Regex literals /.../ should generally be preferred, but fromString
is useful when you need to dynamically construct a regex using strings,
exactly like when you do so in JavaScript.
Examples
RESCRIPTlet firstReScriptFileExtension = (filename, content) => {
let result = Js.Re.fromString(filename ++ "\.(res|resi)")->Js.Re.exec_(content)
switch result {
| Some(r) => Js.Nullable.toOption(Js.Re.captures(r)[1])
| None => None
}
}
// outputs "res"
firstReScriptFileExtension("School", "School.res School.resi Main.js School.bs.js")
fromStringWithFlags
Deprecated
let fromStringWithFlags: (string, ~flags: string) => tConstructs a RegExp object (Js.Re.t) from a string with the given flags.
See Js.Re.fromString.
Valid flags:
g global
i ignore case
m multiline
u unicode (es2015)
y sticky (es2015)
flags
Deprecated
let flags: t => stringReturns the enabled flags as a string.
global
Deprecated
let global: t => boolReturns a bool indicating whether the global flag is set.
ignoreCase
Deprecated
let ignoreCase: t => boolReturns a bool indicating whether the ignoreCase flag is set.
lastIndex
Deprecated
let lastIndex: t => intReturns the index where the next match will start its search. This property will be modified when the RegExp object is used, if the global ("g") flag is set.
Examples
RESCRIPTlet re = /ab*TODO/g
let str = "abbcdefabh"
let break = ref(false)
while !break.contents {
switch Js.Re.exec_(re, str) {
| Some(result) =>
Js.Nullable.iter(Js.Re.captures(result)[0], match_ => {
let next = Belt.Int.toString(Js.Re.lastIndex(re))
Js.log("Found " ++ (match_ ++ (". Next match starts at " ++ next)))
})
| None => break := true
}
}
See
RegExp: lastIndex
on MDN.
setLastIndex
Deprecated
let setLastIndex: (t, int) => unitSets the index at which the next match will start its search from.
multiline
Deprecated
let multiline: t => boolReturns a bool indicating whether the multiline flag is set.
source
Deprecated
let source: t => stringReturns the pattern as a string.
sticky
Deprecated
let sticky: t => boolReturns a bool indicating whether the sticky flag is set.
unicode
Deprecated
let unicode: t => boolReturns a bool indicating whether the unicode flag is set.
exec_
Deprecated
let exec_: (t, string) => option<result>Executes a search on a given string using the given RegExp object.
Returns Some(Js.Re.result) if a match is found, None otherwise.
Examples
RESCRIPT/* Match "quick brown" followed by "jumps", ignoring characters in between
* Remember "brown" and "jumps"
* Ignore case
*/
let re = /quick\s(brown).+?(jumps)/ig
let result = Js.Re.exec_(re, "The Quick Brown Fox Jumps Over The Lazy Dog")
See RegExp.prototype.exec()
on MDN.
test_
Deprecated
let test_: (t, string) => boolTests whether the given RegExp object will match a given string.
Returns true if a match is found, false otherwise.
Examples
RESCRIPT/* A simple implementation of Js.String.startsWith */
let str = "hello world!"
let startsWith = (target, substring) => Js.Re.fromString("^" ++ substring)->Js.Re.test_(target)
Js.log(str->startsWith("hello")) /* prints "true" */
See RegExp.prototype.test()
on MDN.