-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path05_正则的扩展.html
269 lines (183 loc) · 7.6 KB
/
05_正则的扩展.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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script>
// var regex = new RegExp('xyz', 'i');
// console.log(regex);
// var regex = new RegExp(/xyz/i);
// console.log(regex);
// var regex = new RegExp(/xyz/, 'i');
// console.log(regex);
// var regex = new RegExp(/abc/ig, 'i').flags;
// console.log(regex);
// console.log(String.prototype.match);
// console.log(/^\uD83D/u.test('\uD83D\uDC2A'));
// console.log(/^\uD83D/.test('\uD83D\uDC2A'));
// console.log("\uD83D\uDC2A");
// console.log("\uD83D");
// var s = '𠮷';
// console.log(/^.$/.test(s));
// console.log(/^.$/u.test(s));
// console.log(/\u{61}/.test('a'));
// console.log(/\u{61}/u.test('a'));
// console.log(/\u{20BB7}/u.test('𠮷'));
// console.log(/a{2}/.test('aa'));
// console.log(/a{2}/u.test('aa'));
// console.log(/𠮷{2}/.test('𠮷𠮷'));
// console.log(/𠮷{2}/u.test('𠮷𠮷'));
// console.log(/^\S$/.test('𠮷'));
// console.log(/^\S$/u.test('𠮷'));
// function codePointLength(text) {
// var result = text.match(/[\s\S]/gu);
// return result ? result.length : 0;
// }
// var s = '𠮷𠮷';
// console.log(s.length);
// console.log(codePointLength(s));
// console.log(/[a-z]/i.test('\u211A'));
// console.log(/[a-z]/iu.test('\u212A'));
// const r1 = /hello/;
// const r2 = /hello/u;
// console.log(r1.unicode);
// console.log(r2.unicode);
// var s = 'aaa_aa_a';
// var r1 = /a+/g;
// var r2 = /a+/y;
// console.log(r1.exec(s));
// console.log(r2.exec(s));
// console.log(r1.exec(s));
// console.log(r2.exec(s));
// var s = 'aaa_aa_a';
// var r = /a+_/y;
// console.log(r.exec(s));
// console.log(r.exec(s));
// const REGEX = /a/g;
// REGEX.lastIndex = 2;
// const match = REGEX.exec('xaya');
// console.log(match.index);
// console.log(REGEX.lastIndex);
// console.log(REGEX.exec('xaya'));
// const REGEX = /a/y;
// REGEX.lastIndex = 2;
// console.log(REGEX.exec('xaya'));
// REGEX.lastIndex = 3;
// const match = REGEX.exec('xaya');
// console.log(match.index);
// console.log(REGEX.lastIndex);
// console.log(/b/y.exec('aba'));
// const REGEX = /a/gy;
// console.log('aaxa'.replace(REGEX, '-'));
// console.log('a1a2a3'.match(/a\d/y));
// console.log('a1a2a3'.match(/a\d/gy));
// const TOKEN_Y = /\s*(\+|[0-9]+)\s*/y;
// const TOKEN_G = /\s*(\+|[0-9]+)\s*/g;
// console.log(tokenize(TOKEN_Y, '3x + 4'));
// console.log(tokenize(TOKEN_G, '3x + 4'));
// function tokenize(TOKEN_REGEX, str) {
// let result = [];
// let match;
// while (match = TOKEN_REGEX.exec(str)) {
// result.push(match[1]);
// }
// return result;
// }
// var r = /hello\d/y;
// console.log(r.sticky);
// console.log(/abc/is.source);
// console.log(/abc/ig.flags);
// console.log(/foo.bar/.test('foo\nbar'));
// console.log(/foo[^]bar/.test('foo\nbar'));
// console.log(/foo.bar/s.test('foo\nbar'));
// const re = /foo.bar/s;
// console.log(re.test('foo\nbar'));
// console.log(re.dotAll);
// console.log(re.flags);
// console.log(/\d+(?=%)/.exec('100% of US presidents have been male'));
// console.log(/\d+(?!%)/.exec('that’s all 44 of them'));
// console.log(/(?<=\$)\d+/.exec('Benjamin Franklin is on the $100 bill'));
// console.log(/(?<!\$)\d+/.exec('it’s worth about €90'));
// const RE_DOLLAR_PREFIX = /(?<=\$)foo/g;
// console.log('$foo %foo foo'.replace(RE_DOLLAR_PREFIX, 'bar'));
// console.log(/(?<=(\d+)(\d+))$/.exec('1053'));
// console.log(/^(\d+)(\d+)$/.exec('1053'));
// console.log(/(?<=(o)d\1)r/.exec('hodor'));
// console.log(/(?<=\1d(o))r/.exec('hodor'));
// const regexGreekSymbol = /\p{Script=Greek}/u;
// console.log(regexGreekSymbol.test('π'));
// const regex = /^\p{Decimal_Number}+$/u;
// console.log(regex.test('𝟏𝟐𝟑𝟜𝟝𝟞𝟩𝟪𝟫𝟬𝟭𝟮𝟯𝟺𝟻𝟼'));
// const regex = /^\p{Number}+$/u;
// console.log(regex.test('²³¹¼½¾'));
// console.log(regex.test('㉛㉜㉝'));
// console.log(regex.test('ⅠⅡⅢⅣⅤⅥⅦⅧⅨⅩⅪⅫ'));
// const RE_DATE = /(\d{4})-(\d{2})-(\d{2})/;
// const matchObj = RE_DATE.exec('1999-12-31');
// const year = matchObj[1];
// const month = matchObj[2];
// const day = matchObj[3];
// console.log(year);
// console.log(month);
// console.log(day);
// const RE_DATE = /(?<year>\d{4})-(?<month>\d{2})-(?<day>\d{2})/;
// const matchObj = RE_DATE.exec('1999-12-31');
// const year = matchObj.groups.year;
// const month = matchObj.groups.month;
// const day = matchObj.groups.day;
// console.log(year, month, day);
// const RE_OPT_A = /^(?<as>a+)?$/;
// const matchObj = RE_OPT_A.exec('');
// console.log(matchObj.groups.as);
// console.log('as' in matchObj.groups);
// let {groups: {one, two}} = /^(?<one>.*):(?<two>.*)/u.exec('foo:bar');
// console.log(one);
// console.log(two);
// let re = /(?<year>\d{4})-(?<month>\d{2})-(?<day>\d{2})/u;
// console.log('2015-01-02'.replace(re, '$<day>/$<month>/$<year>'));
// const RE_TWICE = /^(?<word>[a-z]+)!\k<word>$/;
// console.log(RE_TWICE.test('abc!abc'));
// console.log(RE_TWICE.test('abc!ab'));
// const RE_TWICE = /^(?<word>[a-z]+)!\1$/;
// console.log(RE_TWICE.test('abc!abc'));
// console.log(RE_TWICE.test('abc!ab'));
// const RE_TWICE = /^(?<word>[a-z]+)!\k<word>!\1$/;
// console.log(RE_TWICE.test('abc!abc!abc'));
// console.log(RE_TWICE.test('abc!abc!ab'));
// const text = 'zabbcdef';
// const re = /ab/d;
// const result = re.exec(text);
// console.log(result.index);
// console.log(result.indices);
// const text = 'zabbcdef';
// const re = /ab+(cd)/d;
// const result = re.exec(text);
// console.log(result.indices);
// const text = 'zabbcdef';
// const re = /ab+(?<Z>cd)/d;
// const result = re.exec(text);
// console.log(result.indices.groups);
// const text = 'zabbcdef';
// const re = /ab+(?<Z>ce)?/d;
// const result = re.exec(text);
// console.log(result.indices[1]);
// console.log(result.indices.groups['Z']);
// var regex = /t(e)(st(\d?))/g;
// var string = 'test1test2test3';
// var matches = [];
// var match;
// while (match = regex.exec(string)) {
// matches.push(match);
// }
// console.log(matches);
// const string = 'test1test2test3';
// const regex = /t(e)(st(\d?))/g;
// for (const match of string.matchAll(regex)) {
// console.log(match);
// }
</script>
</body>
</html>