|
23 | 23 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *)
|
24 | 24 |
|
25 | 25 | (**
|
26 |
| - Provides bindings for JavaScript Regular Expressions |
27 |
| -
|
28 |
| - {4 Syntax sugar} |
29 |
| - ReScript provides a bit of syntax sugar for regex literals: `[%re "/foo/g"]` |
30 |
| - will evaluate to a [` t`]() that can be passed around and used like usual. |
31 |
| -
|
32 |
| - **Note:** This is not an immutable API. A RegExp object with the `global` ("g") |
33 |
| - flag set will modify the [` lastIndex`]() property when the RegExp object is used, |
34 |
| - and subsequent uses will ocntinue the search from the previous [` lastIndex`](). |
35 |
| -
|
36 |
| - ``` |
37 |
| - let maybeMatches = "banana" |> Js.String.match_ [%re "/na+/g"] |
38 |
| - ``` |
39 |
| -
|
40 |
| - **see** [MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp) |
41 |
| -
|
42 |
| - **see** [MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions) |
| 26 | + Provide bindings to JS regular expressions (RegExp). |
43 | 27 | *)
|
44 | 28 |
|
45 |
| -(** the RegExp object *) |
| 29 | +(** The RegExp object. *) |
46 | 30 | type t
|
47 | 31 |
|
48 |
| -(** the result of a executing a RegExp on a string *) |
| 32 | +(** The result of a executing a RegExp on a string. *) |
49 | 33 | type result
|
50 | 34 |
|
51 |
| -(** an array of the match and captures, the first is the full match and the remaining are the substring captures *) |
| 35 | +(** |
| 36 | + An `array` of the match and captures, the first is the full match and the |
| 37 | + remaining are the substring captures. |
| 38 | +*) |
52 | 39 | external captures : result -> string Js.nullable array = "%identity"
|
53 | 40 |
|
54 | 41 | (**
|
55 |
| - an array of the matches, the first is the full match and the remaining are the substring matches |
| 42 | + Deprecated. Use `captures` instead. |
56 | 43 | *)
|
57 | 44 | external matches : result -> string array = "%identity"
|
58 | 45 | [@@deprecated "Use Js.Re.captures instead"]
|
59 | 46 |
|
60 |
| -(** 0-based index of the match in the input string *) |
| 47 | +(** 0-based index of the match in the input string. *) |
61 | 48 | external index : result -> int = "index" [@@bs.get]
|
62 | 49 |
|
63 |
| -(** the original input string *) |
| 50 | +(** The original input string. *) |
64 | 51 | external input : result -> string = "input" [@@bs.get]
|
65 | 52 |
|
66 | 53 |
|
67 | 54 | (**
|
68 |
| - Constructs a RegExp object ([` t`]()) from a string |
69 |
| -
|
70 |
| - Regex literals (`[%re "/.../"]`) should generally be preferred, but |
71 |
| - `fromString` is very useful when you need to insert a string into a regex. |
72 |
| -
|
73 |
| - ``` |
74 |
| - (* A function that extracts the content of the first element with the given tag *) |
75 |
| -
|
76 |
| - let contentOf tag xmlString = |
77 |
| - Js.Re.fromString ("<" ^ tag ^ ">(.*?)<\/" ^ tag ^">") |
78 |
| - |> Js.Re.exec xmlString |
79 |
| - |> function |
80 |
| - | Some result -> Js.Nullable.toOption (Js.Re.captures result).(1) |
81 |
| - | None -> None |
| 55 | + Constructs a RegExp object (Js.Re.t) from a `string`. |
| 56 | + Regex literals `%re("/.../")` should generally be preferred, but `fromString` |
| 57 | + is useful when you need to dynamically construct a regex using strings, |
| 58 | + exactly like when you do so in JavaScript. |
| 59 | +
|
| 60 | + ```res example |
| 61 | + let firstReScriptFileExtension = (filename, content) => { |
| 62 | + let result = Js.Re.fromString(filename ++ "\.(res|resi)")->Js.Re.exec_(content) |
| 63 | + switch result { |
| 64 | + | Some(r) => Js.Nullable.toOption(Js.Re.captures(r)[1]) |
| 65 | + | None => None |
| 66 | + } |
| 67 | + } |
| 68 | +
|
| 69 | + // outputs "res" |
| 70 | + firstReScriptFileExtension("School", "School.res School.resi Main.js School.bs.js") |
82 | 71 | ```
|
83 | 72 | *)
|
84 | 73 | external fromString : string -> t = "RegExp" [@@bs.new]
|
85 | 74 |
|
86 | 75 | (**
|
87 |
| - Constructs a RegExp object ([` t`]()) from a string with the given `flags` |
88 |
| -
|
89 |
| - See [` fromString`]() |
| 76 | + Constructs a RegExp object (`Js.Re.t`) from a string with the given flags. |
| 77 | + See `Js.Re.fromString`. |
90 | 78 |
|
91 | 79 | Valid flags:
|
92 |
| - - g: global |
93 |
| - - i: ignore case |
94 |
| - - m: multiline |
95 |
| - - u: unicode (es2015) |
96 |
| - - y: sticky (es2015) |
97 | 80 |
|
| 81 | + - **g** global |
| 82 | + - **i** ignore case |
| 83 | + - **m** multiline |
| 84 | + - **u** unicode (es2015) |
| 85 | + - **y** sticky (es2015) |
98 | 86 | *)
|
99 | 87 | external fromStringWithFlags : string -> flags:string -> t = "RegExp" [@@bs.new]
|
100 | 88 |
|
101 |
| -(** returns the enabled flags as a string *) |
| 89 | +(** Returns the enabled flags as a string. *) |
102 | 90 | external flags : t -> string = "flags" [@@bs.get]
|
103 | 91 |
|
104 |
| -(** returns a bool indicating whether the `global` flag is set *) |
| 92 | +(** Returns a `bool` indicating whether the global flag is set. *) |
105 | 93 | external global : t -> bool = "global" [@@bs.get]
|
106 | 94 |
|
107 |
| -(** returns a bool indicating whether the `ignoreCase` flag is set *) |
| 95 | +(** Returns a `bool` indicating whether the ignoreCase flag is set. *) |
108 | 96 | external ignoreCase : t -> bool = "ignoreCase" [@@bs.get]
|
109 | 97 |
|
110 | 98 | (**
|
111 |
| - returns the index where the next match will start its search |
112 |
| -
|
113 |
| - This property will be modified when the RegExp object is used, if the `global` ("g") |
114 |
| - flag is set. |
115 |
| -
|
| 99 | + Returns the index where the next match will start its search. This property |
| 100 | + will be modified when the RegExp object is used, if the global ("g") flag is |
| 101 | + set. |
| 102 | +
|
| 103 | + ```res example |
| 104 | + let re = %re("/ab*/g") |
| 105 | + let str = "abbcdefabh" |
| 106 | +
|
| 107 | + let break = ref(false) |
| 108 | + while !break.contents { |
| 109 | + switch Js.Re.exec_(re, str) { |
| 110 | + | Some(result) => Js.Nullable.iter(Js.Re.captures(result)[0], (. match_) => { |
| 111 | + let next = Belt.Int.toString(Js.Re.lastIndex(re)) |
| 112 | + Js.log("Found " ++ (match_ ++ (". Next match starts at " ++ next))) |
| 113 | + }) |
| 114 | + | None => break := true |
| 115 | + } |
| 116 | + } |
116 | 117 | ```
|
117 |
| - (* Finds and prints successive matches *) |
118 |
| -
|
119 |
| - let re = [%re "/ab*/g"] in |
120 |
| - let str = "abbcdefabh" in |
121 |
| -
|
122 |
| - let break = ref false in |
123 |
| - while not !break do |
124 |
| - match re |> Js.Re.exec str with |
125 |
| - | None -> break := true |
126 |
| - | Some result -> |
127 |
| - Js.Nullable.iter (Js.Re.captures result).(0) ((fun match_ -> |
128 |
| - let next = string_of_int (Js.Re.lastIndex re) in |
129 |
| - Js.log ("Found " ^ match_ ^ ". Next match starts at " ^ next))) |
130 |
| - done |
131 |
| - ``` |
132 |
| -
|
133 |
| - **see** [MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/lastIndex) |
134 | 118 | *)
|
135 | 119 | external lastIndex : t -> int = "lastIndex" [@@bs.get]
|
136 | 120 |
|
137 |
| -(** sets the index at which the next match will start its search from *) |
| 121 | +(** Sets the index at which the next match will start its search from. *) |
138 | 122 | external setLastIndex : t -> int -> unit = "lastIndex" [@@bs.set]
|
139 | 123 |
|
140 |
| -(** returns a bool indicating whether the `multiline` flag is set *) |
| 124 | +(** Returns a `bool` indicating whether the multiline flag is set. *) |
141 | 125 | external multiline : t -> bool = "multiline" [@@bs.get]
|
142 | 126 |
|
143 |
| -(** returns the pattern as a string *) |
| 127 | +(** Returns the pattern as a `string`. *) |
144 | 128 | external source : t -> string = "source" [@@bs.get]
|
145 | 129 |
|
146 |
| -(** returns a bool indicating whether the `sticky` flag is set *) |
| 130 | +(** Returns a `bool` indicating whether the sticky flag is set. *) |
147 | 131 | external sticky : t -> bool = "sticky" [@@bs.get]
|
148 | 132 |
|
149 |
| -(** returns a bool indicating whether the `unicode` flag is set *) |
| 133 | +(** Returns a `bool` indicating whether the unicode flag is set. *) |
150 | 134 | external unicode : t -> bool = "unicode" [@@bs.get]
|
151 | 135 |
|
152 | 136 | (**
|
153 |
| - executes a search on a given string using the given RegExp object |
154 |
| -
|
155 |
| - **return** `Some` [` result`]() if a match is found, `None` otherwise |
| 137 | + Executes a search on a given string using the given RegExp object. |
| 138 | + Returns `Some(Js.Re.result)` if a match is found, `None` otherwise. |
156 | 139 |
|
157 |
| - ``` |
158 |
| - (* Match "quick brown" followed by "jumps", ignoring characters in between |
| 140 | + ```res example |
| 141 | + /* Match "quick brown" followed by "jumps", ignoring characters in between |
159 | 142 | * Remember "brown" and "jumps"
|
160 | 143 | * Ignore case
|
161 |
| - *) |
| 144 | + */ |
162 | 145 |
|
163 |
| - let re = [%re "/quick\s(brown).+?(jumps)/ig"] in |
164 |
| - let result = re |. Js.Re.exec_ "The Quick Brown Fox Jumps Over The Lazy Dog" |
| 146 | + let re = %re("/quick\s(brown).+?(jumps)/ig") |
| 147 | + let result = Js.Re.exec_(re, "The Quick Brown Fox Jumps Over The Lazy Dog") |
165 | 148 | ```
|
166 |
| -
|
167 |
| - **see** [MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/exec) |
168 | 149 | *)
|
169 | 150 | external exec_ : t -> string -> result option = "exec" [@@bs.send] [@@bs.return null_to_opt]
|
170 | 151 |
|
171 | 152 |
|
172 | 153 | (**
|
173 |
| - tests whether the given RegExp object will match a given string |
| 154 | + Tests whether the given RegExp object will match a given `string`. |
| 155 | + Returns true if a match is found, false otherwise. |
174 | 156 |
|
175 |
| - **return** `true` if a match is found, `false` otherwise |
176 |
| -
|
177 |
| - ``` |
178 |
| - (* A simple implementation of Js.String.startsWith *) |
| 157 | + ```res example |
| 158 | + /* A simple implementation of Js.String.startsWith */ |
179 | 159 |
|
180 | 160 | let str = "hello world!"
|
181 | 161 |
|
182 |
| - let startsWith target substring = |
183 |
| - Js.Re.fromString ("^" ^ substring) |
184 |
| - |. Js.Re.test_ target |
| 162 | + let startsWith = (target, substring) => |
| 163 | + Js.Re.fromString("^" ++ substring)->Js.Re.test_(target) |
185 | 164 |
|
186 |
| - let () = Js.log (str |. startsWith "hello") (* prints "true" *) |
| 165 | + Js.log(str->startsWith("hello")) /* prints "true" */ |
187 | 166 | ```
|
188 |
| -
|
189 |
| - **see** [MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/test) |
190 | 167 | *)
|
191 | 168 | external test_ : t -> string -> bool = "test" [@@bs.send]
|
0 commit comments