-
Notifications
You must be signed in to change notification settings - Fork 38
Expand file tree
/
Copy pathregexp.jsdoc
More file actions
272 lines (206 loc) · 6.88 KB
/
Copy pathregexp.jsdoc
File metadata and controls
272 lines (206 loc) · 6.88 KB
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
RegExp : Object
RegExp represents a regular expression that can be used for
searching and extracting parts of %%/String|**String**%%s.
Spec:
http://www.ecma-international.org/ecma-262/5.1/#sec-15.10
----
RegExp(pattern : String, [flags : String]) : RegExp
Same as %%#new_RegExp_String_String|**new RegExp(pattern, flags)**%%.
Spec:
http://www.ecma-international.org/ecma-262/5.1/#sec-15.10.3.1
----
new RegExp(pattern : String, [flags : String]) : RegExp
Constructs a new **RegExp** for the specified **pattern**.
See %%#flags|**flags**%% for acceptable flag values.
**RegExp**s can also be constructed using **/pattern/flags** syntax.
<example>
// The following are equivalent
var x = /pattern/i;
var y = RegExp('pattern', 'i');
var z = new RegExp('pattern', 'i');
</example>
Spec:
http://www.ecma-international.org/ecma-262/5.1/#sec-15.10.4.1
----
prototype.exec(str : String) : Array<String>
If **this** matches **str**, returns a new **Array** with item **0** equal to the
portion of **str** that matched the regular expression, item **1** equal to the
first capturing group in **this**, item **2** equal to the second capturing group in
**this**, and so on. If **this** doesn't match **str**, returns **null**. See also
%%/String#match|String.match()%%.
<example>
var regexp = /(\d\d\d)-(\d\d\d\d)/;
var result = regexp.exec('call me: 555-4385');
if (result) {
console.log(result[0]);
console.log(result[1]);
console.log(result[2]);
}
</example>
<p>
You can name capture groups by placing **?<<em>name</em>>** at the start of the group.
The captured values are available on a **groups** property on the returned array. Note,
this is new with ECMAScript 2018.
</p>
<example>
var regexp = /(?<areacode>\d\d\d)-(?<number>\d\d\d-\d\d\d\d)/;
var result = regexp.exec('call me: 333-555-4385');
if (result) {
console.dir(result.groups.areacode);
console.dir(result[1]);
console.dir(result.groups.number);
console.dir(result[2]);
}
</example>
<p>
If **this** is a %%#global|**global**%% RegExp, **exec** can be called repeatedly
to get all matches. Each time it is called, it updates
%%#lastIndex|**this.lastIndex**%% and begins the search from there. When there are
no more matches, returns **null**. See also %%/String#matchAll|String.matchAll()%%.
</p>
<example>
var regexp = /(\w+): (\d+)/g;
var res;
while (res = regexp.exec('a: 12, b: 5, d: 6')) {
console.log(res[1], res[2]);
}
</example>
Spec:
http://www.ecma-international.org/ecma-262/5.1/#sec-15.10.6.2
----
prototype.test(str : String) : Boolean
Returns **true** if **this** matches **str**.
<example>
var regexp = /(\d\d\d)-(\d\d\d\d)/;
console.log(regexp.test('555-4385'));
console.log(regexp.test('KL5-4385'));
</example>
Spec:
http://www.ecma-international.org/ecma-262/5.1/#sec-15.10.6.3
----
instance.source : String
The pattern of this regular expression.
<example>
var regexp = /(\d\d\d)-(\d\d\d\d)/;
console.log(regexp);
console.log(regexp.source);
</example>
ReadOnly:
true
Spec:
http://www.ecma-international.org/ecma-262/5.1/#sec-15.10.7.1
----
instance.dotAll : Boolean
**true** if the **RegExp** was created with the **'s'** flag.
DotAll **RegExp**s will match any character for **'.'** including new lines (**'\r'** or **'\n'**)
which do not match **'.'** by default.
Another option is to replace **'.'** with an empty inverted character class
match **'[^]'** for systems that do not support ECMAScript 2018.
See also %%#multiline|**multiline**%%.
<example>
var str = '"a quote\nover multiple lines"';
// The following fails because . does not match \n without the s suffix
console.log(/".*"/.test(str));
// The s suffix allows . to match \n
console.log(/".*"/s.test(str));
// Alternatively, use the empty inverted character class
console.log(/"[^]*"/.test(str));
</example>
ReadOnly:
true
Version:
ECMAScript 2018
Spec:
http://www.ecma-international.org/ecma-262/5.1/#sec-15.10.7.2
----
instance.flags : String
Returns the flags specified when constructing **this**.
Each character in **flags** represents a different option of the RegExp.
**'g'** is for %%#global|**global**%%.
**'i'** is for %%#ignoreCase|**ignoreCase**%%.
**'m'** is for %%#multiline|**multiline**%%.
**'s'** is for %%#dotAll|**dotAll**%%.
**'y'** is for %%#sticky|**sticky**%%.
<example>
console.log(/./gim.flags);
</example>
ReadOnly:
true
Spec:
http://www.ecma-international.org/ecma-262/5.1/#sec-15.10.7.2
----
instance.global : Boolean
**true** if the **RegExp** was created with the **'g'** flag. Global **RegExp**s
will match each place %%#source|**this.source**%% occurs in the string, not just the
first match. See also %%/String#matchAll|String.matchAll()%%.
<example>
console.log('abcba'.replace(/b/, 'X'));
console.log('abcba'.replace(/b/g, 'X'));
</example>
ReadOnly:
true
Spec:
http://www.ecma-international.org/ecma-262/5.1/#sec-15.10.7.2
----
instance.ignoreCase : Boolean
**true** if the **RegExp** was created with the **'i'** flag. IgnoreCase
**RegExp**s will do case insensitive matching.
<example>
console.log(/hello/.test('Hello'));
console.log(/hello/i.test('Hello'));
</example>
ReadOnly:
true
Spec:
http://www.ecma-international.org/ecma-262/5.1/#sec-15.10.7.3
----
instance.multiline : Boolean
**true** if the **RegExp** was created with the **'m'** flag. Multiline
**RegExp**s will match **'^'** to the beginning of lines as well as the
beginning of the string and match **'$'** to the end of lines as well as
the end of the string. Note that **multiline** does not affect the **'.'**
character class. See %%#dotAll|**dotAll**%% for more details.
<example>
var str = 'hello\nworld';
console.log(/^world/.test(str));
console.log(/^world/m.test(str));
</example>
ReadOnly:
true
Spec:
http://www.ecma-international.org/ecma-262/5.1/#sec-15.10.7.4
----
instance.lastIndex : Number
The starting index into the string for the next search. If **this** is
%%#sticky|**sticky**%%, the match must be found starting exactly at **lastIndex**.
**lastIndex** is automatically set to the found index after a successful match.
This property only applies if the **RegExp** is %%#global|**global**%% or
%%#sticky|**sticky**.
<example>
var regexp = /foo|bar|baz/g;
console.log(regexp.exec('foo bar baz')[0]);
console.log(regexp.lastIndex);
regexp.lastIndex = 5;
console.log(regexp.exec('foo bar baz')[0]);
</example>
Spec:
http://www.ecma-international.org/ecma-262/5.1/#sec-15.10.7.5
----
instance.sticky : Boolean
**true** if the **RegExp** was created with the **'y'** flag. Sticky
**RegExp**s will only look for matches that start exactly at
%%#lastIndex|**lastIndex**%%. Non-sticky **RegExp**s will try finding
a match at all subsequent positions in the string if there's no match
at **lastIndex**.
<example>
var nonSticky = /bar/;
console.log(nonSticky.test('foo bar baz'));
var sticky = /bar/y;
console.log(sticky.test('foo bar baz'));
sticky.lastIndex = 4;
console.log(sticky.test('foo bar baz'));
</example>
ReadOnly:
true
Spec:
https://tc39.es/ecma262/#sec-get-regexp.prototype.sticky