-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathrx1.js
229 lines (187 loc) · 7.23 KB
/
rx1.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
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
//-------------------------------------------------------------------------------------------------------
// Copyright (C) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
//-------------------------------------------------------------------------------------------------------
////////////////////////
//Note RegExp.lastIndex does not exist any more, returning undefined is expected behaviour
function write(o) {
try {
document.write(o + "<br/>");
write = function(o) { document.write(o + "<br/>"); };
} catch (ex) {
try {
WScript.Echo("" + o);
write = function(o) { WScript.Echo("" + o); };
} catch (ex2) {
print("" + o);
write = function(o) { print("" + o); };
}
}
}
var re;
var arr;
var str;
write("******** replace tests");
str = "JavaScript is more fun than Java";
var strResult = str.replace(/\b\w+\b/g, function(word) {
return word.substring(0, 1).toUpperCase() +
word.substring(1);
});
write(strResult);
str = "Doe, John"
write (str.replace(/(\w+)\s*,\s*(\w+)/, "$2 $1"));
str = "$1,$2";
write(str.replace(/(\$(\d))/g, "$$1-$1$2"));
str = "Doe, John Abe, gold C,B alan, bart"
write ("original string: " + str + " replaced string: " +str.replace(new RegExp( '(\\w+)\\s*,\\s*(\\w+)', "g"), "$2 $1"));
write ("original string: " + str + " function replaced string: " +str.replace(new RegExp( '(\\w+)\\s*,\\s*(\\w+)', "g"), function(word, c1, c2, offset, org) {
write( "fn trace : matched str: " + word + " capture1: " + c1 + " capture2: " + c2 + " offset: " + offset + " org str: " + org);
return word.substring(0, 1).toUpperCase() + word.substring(1);
}));
str = "a b";
write ("original string: " + str + " replaced string: " +str.replace(/\s*(\s|$)\s*/g, "$1"));
write("******** split tests");
str = "hello <b>world</b>";
re = /(<[^>]*>)/;
arr = str.split(re); // Returns ["hello ","<b>","world","</b>",""]
WriteAllProps(re, arr);
re = /\s*,\s*/;
arr = "1, 2, 3, 4, 5".split(/\s*,\s*/);
WriteAllProps(re, arr);
re = /a*?/;
arr = "ab".split(re);
WriteAllProps(re, arr);
re = /a*/;
arr = "ab".split(re);
WriteAllProps(re, arr);
re = /<(\/)?([^<>]+)>/;
arr = "A<B>bold</B>and<CODE>coded</CODE>".split(re);
WriteAllProps(re, arr);
write("******** string.match, regexp.exec tests ");
re = new RegExp("d(b+)(d)", "ig");
str = "cdbBdbsbdbdz";
arr = re.exec(str);
WriteAllProps(re, arr);
arr = str.match(re);
WriteAllProps(re, arr);
re.lastIndex = 7;
arr = re.exec(str);
WriteAllProps(re, arr);
re = new RegExp("d(b+)(d)", "i");
arr = str.match(re);
WriteAllProps(re, arr);
s = "cdbBdbsbdbdz";
r = s.match();
write('result match empty: ' + r);
var pattern = /Java/g;
var text = "JavaScript is more fun than Java!";
var result;
while ((result = pattern.exec(text)) != null) {
write("Matched '" + result[0] + "'" +
" at position " + result.index +
"; next search begins at " + pattern.lastIndex);
}
pattern = /dddd|ddd|dd|d|MMMM|MMM|MM|M|yyyy|yy|y|hh|h|HH|H|mm|m|ss|s|tt|t|fff|ff|f|zzz|zz|z/g;
result = pattern.exec("MM/dd/yyyy");
write("Result matching MMMM|MMM|MM|M with MM/dd/yyyy: " + result);
pattern = /aa|a/;
result = pattern.exec("aa");
write("Result match aa|a with aa: " + result);
write("******** test empty regex expressions.");
var r0 = new RegExp("");
write(r0.test("foo"))
var r1 = new RegExp();
write(r1.test("foo"))
write(r1.test(""))
write("******** search tests");
var re1 = /[Jj]ava([Ss]cript)?(?=\:)/;
write(re1.test("JavaScript: The Definitive Guide"))
re = new RegExp("d(b+)(d)", "ig");
r = s.search(re); //Search the string.
write('result search: ' + r);
write(RegExp.input);
write(RegExp.$_);
s = "The rain in Spain falls mainly in the plain.";
re = /falls/mi; //Create regular expression pattern.
r = s.search(re); //Search the string.
write('result search: ' + r);
var re1 = /[Jj]ava([Ss]cript)?(?=\:)/;
write(re1.test("JavaScript: The Definitive Guide"))
function SearchDemo(s, re) {
var r; //Declare variables.
r = s.search(re); //Search the string.
write('result search: ' + r);
WriteAllProps(re, null);
//delete RegExp.input;
RegExp.input = "foooooooooo";
WriteAllProps(re, null);
return (r); //Return the Boolean result.
}
SearchDemo("The rain in Spain falls mainly in the plain.", /falls/mi);
SearchDemo("1@2@3@4@5@6@7@8@9@1@523", /(\d)@(\d)@(\d)@(\d)@(\d)@(\d)@(\d)@(\d)@(\d)@(\d)@(\d+)/g);
write("******** Match Demo indirect names");
function matchDemo(re, pat, v1, v2, v3) {
var s;s
var str = "cdbBdbsbdbdz";
var arr = re.exec(str);
s = "first dolar arg contains: " + pat[v1] + "\n";
s += "second dolar arg contains: " + pat[v2] + "\n";
s += "third dolar arg contains: " + pat[v3];
return (s);
}
// function matchDemo() {
// var s;
// var re = new RegExp("d(b+)(d)", "ig");
// var str = "cdbBdbsbdbdz";
// var arr = re.exec(str);
// s = "$1 contains: " + RegExp.$1 + "\n";
// s += "$2 contains: " + RegExp.$2 + "\n";
// s += "$3 contains: " + RegExp.$3;
// return (s);
// }
var re = new RegExp("d(b+)(d)", "ig");
var sresult = matchDemo(re, RegExp, "$" + "1", "$" + "2", "$" + "3");
write("reg match: " + sresult);
write("******** Misc");
var x = /abc/;
write(x.toString());
write("javscriptSting".search(/[utg]/));
write("jaaaavscriptSaating".match(/a+/));
write("jaaaavscriptSaating".match(/a+?/g));
function WriteAllProps(reg, arr) {
write('regObjInst.lastIndex: ' + reg.lastIndex);
write('regObjInst.Source: ' + reg.source);
write('regObjInst.global: ' + reg.global);
write('regObjInst.ignoreCase: ' + reg.ignoreCase);
write('regObjInst.multiline: ' + reg.multiline);
write('regObjInst.options: ' + reg.options);
write('RegExp.input: ' + RegExp.input);
write('RegExp.input, $_: ' + RegExp.$_);
write('RegExp.index: ' + RegExp.index);
write('RegExp.lastIndex: ' + RegExp.lastIndex);
write('RegExp.lastMatch: ' + RegExp.lastMatch);
write('RegExp.lastMatch, $&: ' + RegExp['$&']);
write('RegExp.lastParen: ' + RegExp.lastParen);
write('RegExp.lastParen $+: ' + RegExp['$+']);
write('RegExp.leftContext: ' + RegExp.leftContext);
write('RegExp.leftContext $`: ' + RegExp['$`']);
write('RegExp.rightContext: ' + RegExp.rightContext);
write('RegExp.rightContext $\': ' + RegExp["$'"]);
write('RegExp.$1: ' + RegExp.$1);
write('RegExp.$2: ' + RegExp.$2);
write('RegExp.$3: ' + RegExp.$3);
write('RegExp.$4: ' + RegExp.$4);
write('RegExp.$5: ' + RegExp.$5);
write('RegExp.$6: ' + RegExp.$6);
write('RegExp.$7: ' + RegExp.$7);
write('RegExp.$8: ' + RegExp.$8);
write('RegExp.$9: ' + RegExp.$9);
write("");
if (arr) {
write('Array : ' + arr);
write('Array input: ' + arr.input);
write('Array index: ' + arr.index);
write('Array lastIndex: ' + arr.lastIndex);
}
write("");
}