Module Js.Re

module Re: Js_re

type t 
the RegExp object
type result 
the result of a executing a RegExp on a string
val matches : result -> string array
an array of the matches, the first is the full match and the remaining are the substring matches
val index : result -> int
0-based index of the match in the input string
val input : result -> string
the original input string
val 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.

(* A function that extracts the content of the first element with the given tag *)

let contentOf tag xmlString =
  Js.Re.fromString ("<" ^ tag ^ ">(.|\n)*?<\/" ^ tag ^">")
    |> Js.Re.exec xmlString
    |> function
      | Some result => Some (Js.Re.matches result).(1)
      | None -> None
val 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)

val flags : t -> string
returns the enabled flags as a string
val global : t -> bool
returns a bool indicating whether the global flag is set
val ignoreCase : t -> bool
returns a bool indicating whether the ignoreCase flag is set
val 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.

See also MDN
(* Finds and prints successive matches *)

let re = [%re "/ab*/g"] in
let str = "abbcdefabh" in

let break = ref false in
while not !break do
  match re |> Js.Re.exec str with
  | None -> break := false
  | Some result ->
    let match_ = (Js.Re.matches result).(0) in
    let next = string_of_int (Js.Re.lastIndex re) in
    Js.log ("Found " ^ match_ ^ ". Next match starts at " ^ next)
done
val multiline : t -> bool
returns a bool indicating whether the multiline flag is set
val source : t -> string
returns the pattern as a string
val sticky : t -> bool
returns a bool indicating whether the sticky flag is set
val unicode : t -> bool
returns a bool indicating whether the unicode flag is set
val exec : string -> t -> result option
executes a search on a given string using the given RegExp object

returns Some Js_re.result if a match is found, None otherwise

See also MDN
(* Match "quick brown" followed by "jumps", ignoring characters in between
 * Remember "brown" and "jumps"
 * Ignore case
 *)

let re = [%re "/quick\s(brown).+?(jumps)/ig" in
let result = re |> Js.Re.exec "The Quick Brown Fox Jumps Over The Lazy Dog"
val test : string -> t -> bool
tests whether the given RegExp object will match a given string

returns true if a match is found, false otherwise

See also MDN
(* A simple implementation of Js.String.startsWith *)

let str = "hello world!"

let startsWith substring target =
  Js.Re.fromString ("^" ^ substring)
    |> Js.Re.test target

let () = Js.log (str |> startsWith "hello") (* prints "true" *)