forked from rescript-lang/rescript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJs.Re.html
315 lines (233 loc) · 13.6 KB
/
Js.Re.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
<!doctype html>
<html>
<head>
<link rel="stylesheet" href="../api_static/tomorrow-night.css">
<link rel="stylesheet" href="../api_static/style.css" type="text/css">
<script src="../api_static//highlight.pack.js"></script>
<script src="../api_static//script.js"></script>
<script>hljs.initHighlightingOnLoad();</script>
<meta charset="utf8">
<link rel="previous" href="Js.Boolean.html"><link rel="next" href="Js.Promise.html">
<title> Js.Re </title></head>
<body>
<nav class="module-index"><ul><li><a href="Bs.html">Bs</a></li>
<li><a href="Bs_dyn.html">Bs_dyn</a></li>
<li><a href="Bs_dyn_lib.html">Bs_dyn_lib</a></li>
<li><a href="Dom.html">Dom</a></li>
<li><a href="Dom_storage.html">Dom_storage</a></li>
<li><a href="Js.html">Js</a></li>
<li><a href="Js_array.html">Js_array</a></li>
<li><a href="Js_boolean.html">Js_boolean</a></li>
<li><a href="Js_cast.html">Js_cast</a></li>
<li><a href="Js_date.html">Js_date</a></li>
<li><a href="Js_dict.html">Js_dict</a></li>
<li><a href="Js_exn.html">Js_exn</a></li>
<li><a href="Js_float.html">Js_float</a></li>
<li><a href="Js_global.html">Js_global</a></li>
<li><a href="Js_int.html">Js_int</a></li>
<li><a href="Js_int64.html">Js_int64</a></li>
<li><a href="Js_internal.html">Js_internal</a></li>
<li><a href="Js_json.html">Js_json</a></li>
<li><a href="Js_list.html">Js_list</a></li>
<li><a href="Js_math.html">Js_math</a></li>
<li><a href="Js_nativeint.html">Js_nativeint</a></li>
<li><a href="Js_null.html">Js_null</a></li>
<li><a href="Js_null_undefined.html">Js_null_undefined</a></li>
<li><a href="Js_obj.html">Js_obj</a></li>
<li><a href="Js_option.html">Js_option</a></li>
<li><a href="Js_primitive.html">Js_primitive</a></li>
<li><a href="Js_promise.html">Js_promise</a></li>
<li><a href="Js_re.html">Js_re</a></li>
<li><a href="Js_result.html">Js_result</a></li>
<li><a href="Js_string.html">Js_string</a></li>
<li><a href="Js_typed_array.html">Js_typed_array</a></li>
<li><a href="Js_types.html">Js_types</a></li>
<li><a href="Js_undefined.html">Js_undefined</a></li>
<li><a href="Js_unsafe.html">Js_unsafe</a></li>
<li><a href="Js_vector.html">Js_vector</a></li>
<li><a href="Node.html">Node</a></li>
<li><a href="Node_buffer.html">Node_buffer</a></li>
<li><a href="Node_child_process.html">Node_child_process</a></li>
<li><a href="Node_fs.html">Node_fs</a></li>
<li><a href="Node_module.html">Node_module</a></li>
<li><a href="Node_path.html">Node_path</a></li>
<li><a href="Node_process.html">Node_process</a></li></ul></nav>
<div class="navbar"><a class="pre" href="Js.Boolean.html" title="Js.Boolean">Previous</a>
<a class="up" href="Js.html" title="Js">Up</a>
<a class="post" href="Js.Promise.html" title="Js.Promise">Next</a>
</div>
<h1>Module <a href="type_Js.Re.html">Js.Re</a></h1>
<pre><span class="keyword">module</span> Re: <code class="type"><a href="Js_re.html">Js_re</a></code></pre><div class="info module top">
<div class="not-examples">
Provide bindings to Js regex expression<br>
</div>
</div>
<hr width="100%">
<div class="type-declaration">
<pre><span id="TYPEt"><span class="keyword">type</span> <code class="type"></code>t</span> </pre>
</div>
<div class="info ">
<div class="not-examples">
the RegExp object<br>
</div>
</div>
<div class="type-declaration">
<pre><span id="TYPEresult"><span class="keyword">type</span> <code class="type"></code>result</span> </pre>
</div>
<div class="info ">
<div class="not-examples">
the result of a executing a RegExp on a string<br>
</div>
</div>
<pre><span id="VALmatches"><span class="keyword">val</span> matches</span> : <code class="type"><a href="Js_re.html#TYPEresult">result</a> -> string array</code></pre><div class="info ">
<div class="not-examples">
an array of the matches, the first is the full match and the remaining are the substring matches<br>
</div>
</div>
<pre><span id="VALindex"><span class="keyword">val</span> index</span> : <code class="type"><a href="Js_re.html#TYPEresult">result</a> -> int</code></pre><div class="info ">
<div class="not-examples">
0-based index of the match in the input string<br>
</div>
</div>
<pre><span id="VALinput"><span class="keyword">val</span> input</span> : <code class="type"><a href="Js_re.html#TYPEresult">result</a> -> string</code></pre><div class="info ">
<div class="not-examples">
the original input string<br>
</div>
</div>
<pre><span id="VALfromString"><span class="keyword">val</span> fromString</span> : <code class="type">string -> <a href="Js_re.html#TYPEt">t</a></code></pre><div class="info ">
<div class="not-examples">
Constructs a RegExp object (<a href="Js_re.html#TYPEt"><code class="code">Js_re.t</code></a>) from a string
<p>
Regex literals (<code class="code">[%re "/.../"]</code>) should generally be preferred, but
<code class="code">fromString</code> is very useful when you need to insert a string into a regex.<br>
</div>
<div class="examples">
<pre class="example"> <pre class="codepre"><code class="code">(* 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
</code></pre> </pre>
</div>
</div>
<pre><span id="VALfromStringWithFlags"><span class="keyword">val</span> fromStringWithFlags</span> : <code class="type">string -> flags:string -> <a href="Js_re.html#TYPEt">t</a></code></pre><div class="info ">
<div class="not-examples">
Constructs a RegExp object (<a href="Js_re.html#TYPEt"><code class="code">Js_re.t</code></a>) from a string with the given <code class="code">flags</code>
<p>
See <a href="Js_re.html#VALfromString"><code class="code">Js_re.fromString</code></a>
<p>
Valid flags:
<table>
<tr> <td>g <td>global
<tr> <td>i <td>ignore case
<tr> <td>m <td>multiline
<tr> <td>u <td>unicode <td>(es2015)
<tr> <td>y <td>sticky <td>(es2015)
</table>
<br>
</div>
</div>
<pre><span id="VALflags"><span class="keyword">val</span> flags</span> : <code class="type"><a href="Js_re.html#TYPEt">t</a> -> string</code></pre><div class="info ">
<div class="not-examples">
returns the enabled flags as a string<br>
</div>
</div>
<pre><span id="VALglobal"><span class="keyword">val</span> global</span> : <code class="type"><a href="Js_re.html#TYPEt">t</a> -> bool</code></pre><div class="info ">
<div class="not-examples">
returns a bool indicating whether the <code class="code">global</code> flag is set<br>
</div>
</div>
<pre><span id="VALignoreCase"><span class="keyword">val</span> ignoreCase</span> : <code class="type"><a href="Js_re.html#TYPEt">t</a> -> bool</code></pre><div class="info ">
<div class="not-examples">
returns a bool indicating whether the <code class="code">ignoreCase</code> flag is set<br>
</div>
</div>
<pre><span id="VALlastIndex"><span class="keyword">val</span> lastIndex</span> : <code class="type"><a href="Js_re.html#TYPEt">t</a> -> int</code></pre><div class="info ">
<div class="not-examples">
returns the index where the next match will start its search
<p>
This property will be modified when the RegExp object is used, if the <code class="code">global</code> ("g")
flag is set.<br>
<div class="tag"> <b>See also</b> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/lastIndex">MDN</a><br>
</div>
</div>
<div class="examples">
<pre class="example"> <pre class="codepre"><code class="code">(* 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 := true
| 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
</code></pre> </pre>
</div>
</div>
<pre><span id="VALsetLastIndex"><span class="keyword">val</span> setLastIndex</span> : <code class="type"><a href="Js_re.html#TYPEt">t</a> -> int -> unit</code></pre><div class="info ">
<div class="not-examples">
sets the index at which the next match will start its search from<br>
</div>
</div>
<pre><span id="VALmultiline"><span class="keyword">val</span> multiline</span> : <code class="type"><a href="Js_re.html#TYPEt">t</a> -> bool</code></pre><div class="info ">
<div class="not-examples">
returns a bool indicating whether the <code class="code">multiline</code> flag is set<br>
</div>
</div>
<pre><span id="VALsource"><span class="keyword">val</span> source</span> : <code class="type"><a href="Js_re.html#TYPEt">t</a> -> string</code></pre><div class="info ">
<div class="not-examples">
returns the pattern as a string<br>
</div>
</div>
<pre><span id="VALsticky"><span class="keyword">val</span> sticky</span> : <code class="type"><a href="Js_re.html#TYPEt">t</a> -> bool</code></pre><div class="info ">
<div class="not-examples">
returns a bool indicating whether the <code class="code">sticky</code> flag is set<br>
</div>
</div>
<pre><span id="VALunicode"><span class="keyword">val</span> unicode</span> : <code class="type"><a href="Js_re.html#TYPEt">t</a> -> bool</code></pre><div class="info ">
<div class="not-examples">
returns a bool indicating whether the <code class="code">unicode</code> flag is set<br>
</div>
</div>
<pre><span id="VALexec"><span class="keyword">val</span> exec</span> : <code class="type">string -> <a href="Js_re.html#TYPEt">t</a> -> <a href="Js_re.html#TYPEresult">result</a> option</code></pre><div class="info ">
<div class="not-examples">
executes a search on a given string using the given RegExp object
<p>
<b>returns</b> <code class="code">Some</code> <a href="Js_re.html#TYPEresult"><code class="code">Js_re.result</code></a> if a match is found, <code class="code">None</code> otherwise<br>
<div class="tag"> <b>See also</b> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/exec">MDN</a><br>
</div>
</div>
<div class="examples">
<pre class="example"> <pre class="codepre"><code class="code">(* 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"
</code></pre> </pre>
</div>
</div>
<pre><span id="VALtest"><span class="keyword">val</span> test</span> : <code class="type">string -> <a href="Js_re.html#TYPEt">t</a> -> bool</code></pre><div class="info ">
<div class="not-examples">
tests whether the given RegExp object will match a given string
<p>
<b>returns</b> <code class="code">true</code> if a match is found, <code class="code">false</code> otherwise<br>
<div class="tag"> <b>See also</b> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/test">MDN</a><br>
</div>
</div>
<div class="examples">
<pre class="example"> <pre class="codepre"><code class="code">(* 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" *)
</code></pre> </pre>
</div>
</div>
</body></html>