Skip to content

Commit e2333b8

Browse files
committed
Added support for XRegExp
1 parent 01e168b commit e2333b8

File tree

2 files changed

+2341
-7
lines changed

2 files changed

+2341
-7
lines changed

app.js

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ var querystring = require('querystring');
55
var util = require("util");
66
var url = require("url");
77

8+
// Hack, but I don't want to make *any* changes to xregexp-all.js
9+
eval(fs.readFileSync('xregexp-all.js').toString());
10+
11+
812
function h(unsafe)
913
{
1014
if (unsafe == null)
@@ -100,6 +104,11 @@ function serveTestPost(request, response)
100104
});
101105
}
102106

107+
function makeRegExp(use_xregexp, str_regex, str_options)
108+
{
109+
return use_xregexp ? new XRegExp(str_regex, str_options) : new RegExp(str_regex, str_options)
110+
}
111+
103112
function serveTest(query, response)
104113
{
105114
response.writeHead(200, {
@@ -159,6 +168,7 @@ function serveTest(query, response)
159168
var replacement = 'replacement' in params ? params['replacement'][0] : null;
160169
var str_options = "";
161170
var options = 'option' in params ? params['option'] : null;
171+
var use_xregexp = 'engine' in params && params['engine'][0] == 'xregexp';
162172
var global = false;
163173
if (options != null && options.length > 0)
164174
{
@@ -179,9 +189,25 @@ function serveTest(query, response)
179189
{
180190
str_options += "i";
181191
}
192+
else if (use_xregexp && option == "dotall")
193+
{
194+
str_options += 's';
195+
}
196+
else if (use_xregexp && option == "comment")
197+
{
198+
str_options += 'x';
199+
}
200+
else if (use_xregexp && option == "explicitcapture")
201+
{
202+
str_options += 'n';
203+
}
204+
else if (use_xregexp && option == "sticky")
205+
{
206+
str_options += 'y';
207+
}
182208
else
183209
{
184-
retVal["warning"] = "Invalid option '" + option + "'";
210+
retVal["warning"] = ('warning' in retVal ? retVal['warning'] : '') + "Invalid option '" + option + "'";
185211
}
186212
}
187213
}
@@ -214,14 +240,14 @@ function serveTest(query, response)
214240

215241
try
216242
{
217-
compileTest = new RegExp(str_regex, str_options);
243+
compileTest = makeRegExp(use_xregexp, str_regex, str_options);
218244
}
219245
catch (err)
220246
{
221247
html.push('<div class="alert alert-error">Error: ');
222248
html.push(h(err.message));
223249
html.push("</div>");
224-
response.write(JSON.stringify({"success": true, "message": "unable to create RegExp object", "html": html.join("")}));
250+
response.write(JSON.stringify({"success": true, "message": "unable to create " + (use_xregexp ? "XRegExp" : "RegExp") + " object", "html": html.join("")}));
225251
response.end();
226252
return;
227253
}
@@ -267,11 +293,11 @@ function serveTest(query, response)
267293
html.push("</td>\n");
268294

269295
html.push('\t\t\t<td>');
270-
html.push(h(input.replace(new RegExp(str_regex, str_options), replacement == null ? "" : replacement)));
296+
html.push(h(input.replace(makeRegExp(use_xregexp, str_regex, str_options), replacement == null ? "" : replacement)));
271297
html.push("</td>\n");
272298

273299
html.push('\t\t\t<td>');
274-
var splits = input.split(new RegExp(str_regex, str_options));
300+
var splits = input.split(makeRegExp(use_xregexp, str_regex, str_options));
275301
for (var split = 0; split < splits.length; split++)
276302
{
277303
html.push("[");
@@ -283,10 +309,10 @@ function serveTest(query, response)
283309
html.push("</td>\n");
284310

285311
html.push('\t\t\t<td>');
286-
html.push(new RegExp(str_regex, str_options).test(input) ? "true" : "false"); // can't use the same object twice
312+
html.push(makeRegExp(use_xregexp, str_regex, str_options).test(input) ? "true" : "false"); // can't use the same object twice
287313
html.push("</td>\n");
288314

289-
var regex = new RegExp(str_regex, str_options);
315+
var regex = makeRegExp(use_xregexp, str_regex, str_options);
290316
var result = regex.exec(input);
291317
if (result == null)
292318
{

0 commit comments

Comments
 (0)