-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathregex1.js
156 lines (124 loc) · 3.61 KB
/
regex1.js
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
//-------------------------------------------------------------------------------------------------------
// Copyright (C) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
//-------------------------------------------------------------------------------------------------------
function write(v) { WScript.Echo(v + ""); }
var groups = {};
function Assert(condition, category)
{
if (!groups[category]) {
groups[category] = 1;
} else {
groups[category]++;
}
if (!condition) {
write(category + " test " + groups[category] + " failed");
} else {
write(category + " test " + groups[category] + " passed");
}
}
write("regex test1");
var re = /a/;
var str = new String("abcda");
if (re.test(str)) {
write(" regex.test pass");
}
else {
write(" regex.test fail");
}
if (str.match(re)) {
write(" string.match pass");
}
else {
write(" string.match fail");
}
var array = re.exec(str);
write(" string.exec : " + array);
var s = "";
var a = s.split(/\s+/);
write("a.length : " + a.length);
write("a[0]:" + a[0]);
var reTemp = /abc/i;
var re = new RegExp(reTemp, "g");
var tmp = "abcdef".replace(re, "");
Assert(re.lastIndex == 0, "lastIndex");
var re = new RegExp(/abc/i, "g");
var tmp = "abcdef".match(re);
Assert(re.lastIndex == 0, "lastIndex");
var re = new RegExp(/abc/g);
re.exec("abcdef");
Assert(re.lastIndex == 3, "lastIndex");
var re = /abc/;
re.exec("abcdef");
Assert(re.lastIndex == 0, "lastIndex");
var re = new RegExp(/abc/g, "i");
Assert(re.global == false, "global");
Assert(re.ignoreCase == true, "ignoreCase");
var re = /abc/i;
var re1 = new RegExp(re, "gm");
Assert(re.global == false, "global");
Assert(re.multiline == false, "multiline");
Assert(re.ignoreCase == true, "ignoreCase");
Assert(re1.global == true, "global");
Assert(re1.multiline == true, "multiline");
Assert(re1.ignoreCase == false, "ignoreCase");
var exceptionThrown = false;
try
{
var re = new RegExp(/a/g, "ab");
}
catch (ex)
{
exceptionThrown = true;
}
Assert(exceptionThrown, "invalid flags");
var re = /(ab)/g
"abc ".match(re);
Assert(RegExp.$1 == "ab", "lastIndex");
var re = /test/;
var exceptionThrown = false;
try
{
re.lastIndex = { toString: function() { throw "an exception string"; } }
}
catch (ex)
{
exceptionThrown = true;
}
Assert(exceptionThrown == false, "lastIndex");
exceptionThrown = false;
try
{
Write("LastIndex is " + re.lastIndex);
}
catch (ex)
{
exceptionThrown = true;
}
Assert(exceptionThrown == true, "lastIndex");
function testsc(r, s) {
if (!r.test(s))
write("invalid interpretation of '" + r + "'");
}
testsc(/^\cA$/, "\x01");
testsc(/^[\cA]$/, "\x01");
testsc(/^\c1$/, "\\c1");
testsc(/^\c$/, "\\c");
testsc(/\c/, "\\c");
testsc(/^\c\1$/, "\\c\x01");
testsc(/\c/, "\\c");
testsc(/^[\c1]$/, "\x11");
testsc(/^[\c]$/, "c");
testsc(/^[\c]]$/, "c]");
testsc(/^[\c-e]+$/, "cde");
//Octal handling
testsc(/^\777$/, "\x3F7");
testsc(/^\777$/, "\777");
testsc(/^\170$/, "x");
//Octal handling test for values > 127
c=[/[\300-\306]/g,"A",/[\340-\346]/g,"a",/\307/g,"C",/\347/g,"c",/[\310-\313]/g,"E",/[\350-\353]/g,"e",/[\314-\317]/g,"I",/[\354-\357]/g,"i",/\321/g,"N",/\361/g,"n",/[\322-\330]/g,"O",/[\362-\370]/g,"o",/[\331-\334]/g,"U",/[\371-\374]/g,"u"];
//Negation of empty char set [^] test
write("aa".match(/([^])(\1)/));
write(/^.+ ab/g.exec(" ab"))
write(/^.+a /.exec("a "))
write(/^.+ax/.exec("ax"))