Skip to content

Commit 01e168b

Browse files
committed
Fixed problem with non-global infinite loop, other minor fixes
1 parent 26e64eb commit 01e168b

File tree

1 file changed

+36
-19
lines changed

1 file changed

+36
-19
lines changed

app.js

Lines changed: 36 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ function serveTest(query, response)
113113
{
114114
response.write(JSON.stringify({"success": false, "message": "no input"}));
115115
response.end();
116+
return;
116117
}
117118

118119
var retVal = {};
@@ -123,9 +124,6 @@ function serveTest(query, response)
123124

124125
try
125126
{
126-
127-
128-
129127
var params = {};
130128
var pairs = query.split("&");
131129
for (var loop = 0; loop < pairs.length; loop++)
@@ -155,11 +153,13 @@ function serveTest(query, response)
155153
{
156154
response.write(JSON.stringify({"success": false, "message": "No regex to test!"}));
157155
response.end();
156+
return;
158157
}
159158

160159
var replacement = 'replacement' in params ? params['replacement'][0] : null;
161160
var str_options = "";
162161
var options = 'option' in params ? params['option'] : null;
162+
var global = false;
163163
if (options != null && options.length > 0)
164164
{
165165
for (var loop = 0; loop < options.length; loop++)
@@ -168,6 +168,7 @@ function serveTest(query, response)
168168

169169
if (option == "global")
170170
{
171+
global = true;
171172
str_options += "g";
172173
}
173174
else if (option == "multiline")
@@ -209,11 +210,11 @@ function serveTest(query, response)
209210
html.push("\t</tr>\n");
210211
html.push("</table>\n");
211212

212-
var regex = null;
213+
var compileTest = null;
213214

214215
try
215216
{
216-
regex = new RegExp(str_regex, str_options);
217+
compileTest = new RegExp(str_regex, str_options);
217218
}
218219
catch (err)
219220
{
@@ -222,6 +223,7 @@ function serveTest(query, response)
222223
html.push("</div>");
223224
response.write(JSON.stringify({"success": true, "message": "unable to create RegExp object", "html": html.join("")}));
224225
response.end();
226+
return;
225227
}
226228

227229
html.push('<table class=\"table table-bordered table-striped\">\n');
@@ -230,10 +232,11 @@ function serveTest(query, response)
230232
html.push("\t\t<tr>\n");
231233
html.push("\t\t\t<th style=\"text-align:center;\">Test</th>\n");
232234
html.push("\t\t\t<th>Input</th>");
233-
html.push("\t\t\t<th>.test()</th>");
235+
html.push("\t\t\t<th>input.replace</th>");
236+
html.push("\t\t\t<th>input.split</th>");
237+
html.push("\t\t\t<th>regex.test()</th>");
234238
html.push("\t\t\t<th>regex.exec().index</th>");
235-
html.push("\t\t\t<th>regex.exec()[0]</th>");
236-
html.push("\t\t\t<th>regex.exec()[1..n]</th>");
239+
html.push("\t\t\t<th>regex.exec()[]</th>");
237240
html.push("\t\t\t<th>regex.lastIndex</th>");
238241
html.push("\t\t</tr>\n");
239242
html.push("\t</thead>\n");
@@ -263,14 +266,31 @@ function serveTest(query, response)
263266
html.push(h(input));
264267
html.push("</td>\n");
265268

269+
html.push('\t\t\t<td>');
270+
html.push(h(input.replace(new RegExp(str_regex, str_options), replacement == null ? "" : replacement)));
271+
html.push("</td>\n");
272+
273+
html.push('\t\t\t<td>');
274+
var splits = input.split(new RegExp(str_regex, str_options));
275+
for (var split = 0; split < splits.length; split++)
276+
{
277+
html.push("[");
278+
html.push(split);
279+
html.push("]: ");
280+
html.push(splits[split] == null ? "<i>(null)</i>" : h(splits[split]));
281+
html.push("<br/>");
282+
}
283+
html.push("</td>\n");
284+
266285
html.push('\t\t\t<td>');
267286
html.push(new RegExp(str_regex, str_options).test(input) ? "true" : "false"); // can't use the same object twice
268287
html.push("</td>\n");
269288

289+
var regex = new RegExp(str_regex, str_options);
270290
var result = regex.exec(input);
271291
if (result == null)
272292
{
273-
html.push('\t\t\t<td colspan="4"><i>(null)</i></td>\n');
293+
html.push('\t\t\t<td colspan="6"><i>(null)</i></td>\n');
274294
}
275295
else
276296
{
@@ -285,7 +305,7 @@ function serveTest(query, response)
285305
else
286306
{
287307
html.push("</tr>\n");
288-
html.push('\t\t\t<td colspan="3" style="text-align:right;">');
308+
html.push('\t\t\t<td colspan="5" style="text-align:right;">');
289309
html.push("regex.exec()");
290310
html.push("</td>\n");
291311
}
@@ -295,14 +315,11 @@ function serveTest(query, response)
295315
html.push("</td>\n");
296316

297317
html.push('\t\t\t<td>');
298-
html.push(result[0]);
299-
html.push("</td>\n");
300-
301-
html.push('\t\t\t<td>');
302-
for (var capture = 1; capture < result.length; capture++)
318+
for (var capture = 0; capture < result.length; capture++)
303319
{
320+
html.push("[");
304321
html.push(capture);
305-
html.push(": ");
322+
html.push("]: ");
306323
html.push(result[capture] == null ? "<i>(null)</i>" : h(result[capture]));
307324
html.push("<br/>");
308325
}
@@ -312,10 +329,10 @@ function serveTest(query, response)
312329
html.push(regex.lastIndex);
313330
html.push("</td>\n");
314331

315-
result = regex.exec(input);
332+
result = global ? regex.exec(input) : null;
316333
}
317-
}
318334

335+
}
319336
html.push("\t\t</tr>\n");
320337
count++;
321338
}
@@ -324,7 +341,7 @@ function serveTest(query, response)
324341
if (count == 0)
325342
{
326343
html.push("\t\t<tr>\n");
327-
html.push('\t\t<td colspan="4"><i>');
344+
html.push('\t\t<td colspan="8"><i>');
328345
html.push("(no input to test)");
329346
html.push("</i></td>\n");
330347
html.push("\t\t</tr>\n");

0 commit comments

Comments
 (0)