You are currently looking at the v6.0 - v8.2 docs (Reason v3.6 syntax edition). You can find the latest API docs here.
(These docs cover all versions between v3 to v8 and are equivalent to the old BuckleScript docs before the rebrand)
Re
Provide bindings to JS regular expressions (RegExp).
t
REtype t
The RegExp object.
result
REtype result
The result of a executing a RegExp on a string.
captures
RElet captures: result => array(Js.nullable(string));
An array
of the match and captures, the first is the full match and the remaining are the substring captures.
matches
RElet matches: result => array(string);
Deprecated. Use captures instead.
An array
of the matches, the first is the full match and the remaining are the substring matches.
index
RElet index: result => int;
0-based index of the match in the input string.
input
RElet input: result => string;
The original input string.
fromString
RElet fromString: string => t;
Constructs a RegExp object (Js.Re.t) from a string
.
Regex literals ([%re "/.../"]) should generally be preferred, but fromString is very useful when you need to insert a string into a regex.
RE/* A function that extracts the content of the first element with the given tag */
let contentOf = (tag, xmlString) =>
Js.Re.fromString("<" ++ tag ++ ">(.*?)<\\/" ++ tag ++ ">")
-> Js.Re.exec_(xmlString)
-> (
fun
| Some(result) => Js.Nullable.toOption(Js.Re.captures(result)[1])
| None => None
);
fromStringWithFlags
RElet fromStringWithFlags: (string, ~flags: string) => t;
Constructs 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
RElet flags: t => string;
Returns the enabled flags as a string.
global
RElet global: t => bool;
Returns a bool
indicating whether the global flag is set.
ignoreCase
RElet ignoreCase: t => bool;
Returns a bool
indicating whether the ignoreCase flag is set.
lastIndex
RElet lastIndex: t => int;
Returns 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.
RElet re = [%re "/ab*/g"];
let str = "abbcdefabh";
let break = ref(false);
while (! break^) {
switch (Js.Re.exec_(re, str)) {
| Some(result) =>
Js.Nullable.iter(
Js.Re.captures(result)[0],
[@bs] match => {
let next = Belt.Int.toString(Js.Re.lastIndex(re));
Js.log("Found " ++ match ++ ". Next match starts at " ++ next);
},
)
| None => break := true
};
};
setLastIndex
RElet setLastIndex: (t, int) => unit;
Sets the index at which the next match will start its search from.
multiline
RElet multiline: t => bool;
Returns a bool
indicating whether the multiline flag is set.
source
RElet source: t => string;
Returns the pattern as a string
.
sticky
RElet sticky: t => bool;
Returns a bool
indicating whether the sticky flag is set.
unicode
RElet unicode: t => bool;
Returns a bool
indicating whether the unicode flag is set.
exec_
RElet 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.
RE/* Match "quick brown" followed by "jumps", ignoring characters in between
* Remember "brown" and "jumps"
* Ignore case
*/
let re = [%re "/quick\s(brown).+?(jumps)/ig"];
let result = (Js.Re.exec_(re, "The Quick Brown Fox Jumps Over The Lazy Dog"));
exec
RElet exec: (string, t) => option(result);
Deprecated. please use Js.Re.exec_
instead.
test_
RElet test_: (t, string) => bool;
Tests whether the given RegExp object will match a given string
.
Returns true if a match is found, false otherwise.
RE/* 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" */
test
RElet test: (string, t) => bool;
Deprecated. please use Js.Re.test_
instead.