@@ -6,8 +6,6 @@ var util = require("util");
6
6
var url = require ( "url" ) ;
7
7
var XRegExp = require ( './xregexp-all.js' ) ;
8
8
9
- XRegExp . install ( 'natives' ) ;
10
-
11
9
function h ( unsafe )
12
10
{
13
11
if ( unsafe == null )
@@ -267,7 +265,7 @@ function serveTest(query, response)
267
265
268
266
try
269
267
{
270
- compileTest = new XRegExp ( str_regex , str_options ) ;
268
+ compileTest = XRegExp ( str_regex , str_options ) ;
271
269
}
272
270
catch ( err )
273
271
{
@@ -278,7 +276,7 @@ function serveTest(query, response)
278
276
html . push ( '</td>\n' ) ;
279
277
html . push ( '\t</tr>\n' ) ;
280
278
html . push ( '</table>\n' ) ;
281
- response . write ( JSON . stringify ( { "success" : false , "message" : "unable to create XRegExp object" , "html" : html . join ( "" ) } ) ) ;
279
+ response . write ( JSON . stringify ( { "success" : false , "message" : "Unable to create XRegExp object: " + err . message , "html" : html . join ( "" ) } ) ) ;
282
280
response . end ( ) ;
283
281
return ;
284
282
}
@@ -290,11 +288,11 @@ function serveTest(query, response)
290
288
html . push ( "\t\t<tr>\n" ) ;
291
289
html . push ( "\t\t\t<th style=\"text-align:center;\">Test</th>\n" ) ;
292
290
html . push ( "\t\t\t<th>Input</th>" ) ;
293
- html . push ( "\t\t\t<th>input .replace()</th>" ) ;
294
- html . push ( "\t\t\t<th>input .split()[]</th>" ) ;
295
- html . push ( "\t\t\t<th>regex .test()</th>" ) ;
296
- html . push ( "\t\t\t<th>regex .exec().index</th>" ) ;
297
- html . push ( "\t\t\t<th>regex .exec()[]</th>" ) ;
291
+ html . push ( "\t\t\t<th>XRegExp .replace()</th>" ) ;
292
+ html . push ( "\t\t\t<th>XRegExp .split()[]</th>" ) ;
293
+ html . push ( "\t\t\t<th>XRegExp .test()</th>" ) ;
294
+ html . push ( "\t\t\t<th>XRegExp .exec().index</th>" ) ;
295
+ html . push ( "\t\t\t<th>XRegExp .exec()[]</th>" ) ;
298
296
html . push ( "\t\t\t<th>regex.lastIndex</th>" ) ;
299
297
html . push ( "\t\t</tr>\n" ) ;
300
298
html . push ( "\t</thead>\n" ) ;
@@ -325,11 +323,11 @@ function serveTest(query, response)
325
323
html . push ( "</td>\n" ) ;
326
324
327
325
html . push ( '\t\t\t<td>' ) ;
328
- html . push ( h ( input . replace ( new XRegExp ( str_regex , str_options ) , replacement == null ? "" : replacement ) ) ) ;
326
+ html . push ( h ( XRegExp . replace ( input , XRegExp ( str_regex , str_options ) , replacement == null ? "" : replacement ) ) ) ;
329
327
html . push ( "</td>\n" ) ;
330
328
331
329
html . push ( '\t\t\t<td>' ) ;
332
- var splits = input . split ( new XRegExp ( str_regex , str_options ) ) ;
330
+ var splits = XRegExp . split ( input , XRegExp ( str_regex , str_options ) ) ;
333
331
for ( var split = 0 ; split < splits . length ; split ++ )
334
332
{
335
333
html . push ( "[" ) ;
@@ -341,11 +339,11 @@ function serveTest(query, response)
341
339
html . push ( "</td>\n" ) ;
342
340
343
341
html . push ( '\t\t\t<td>' ) ;
344
- html . push ( new XRegExp ( str_regex , str_options ) . test ( input ) ? "true" : "false" ) ; // can't use the same object twice
342
+ html . push ( XRegExp . test ( input , XRegExp ( str_regex , str_options ) ) ? "true" : "false" ) ;
345
343
html . push ( "</td>\n" ) ;
346
344
347
- var regex = new XRegExp ( str_regex , str_options ) ;
348
- var result = regex . exec ( input ) ;
345
+ var regex = XRegExp ( str_regex , str_options ) ;
346
+ var result = XRegExp . exec ( input , regex ) ;
349
347
if ( result == null )
350
348
{
351
349
html . push ( '\t\t\t<td colspan="6"><i>(null)</i></td>\n' ) ;
@@ -364,7 +362,7 @@ function serveTest(query, response)
364
362
{
365
363
html . push ( "</tr>\n" ) ;
366
364
html . push ( '\t\t\t<td colspan="5" style="text-align:right;">' ) ;
367
- html . push ( "regex .exec()" ) ;
365
+ html . push ( "XRegExp .exec()" ) ;
368
366
html . push ( "</td>\n" ) ;
369
367
}
370
368
@@ -381,13 +379,28 @@ function serveTest(query, response)
381
379
html . push ( result [ capture ] == null ? "<i>(null)</i>" : h ( result [ capture ] ) ) ;
382
380
html . push ( "<br/>" ) ;
383
381
}
382
+ if ( result . groups ) {
383
+ var captureNames = Object . keys ( result . groups ) ;
384
+ for ( var namedCapture = 0 ; namedCapture < captureNames . length ; namedCapture ++ )
385
+ {
386
+ var key = captureNames [ namedCapture ] ;
387
+ html . push ( "groups." ) ;
388
+ html . push ( key ) ;
389
+ html . push ( ": " ) ;
390
+ html . push ( result . groups [ key ] == null ? "<i>(null)</i>" : h ( result . groups [ key ] ) ) ;
391
+ html . push ( "<br/>" ) ;
392
+ }
393
+ }
384
394
html . push ( "</td>\n" ) ;
385
395
386
396
html . push ( '\t\t\t<td>' ) ;
387
397
html . push ( regex . lastIndex ) ;
388
398
html . push ( "</td>\n" ) ;
389
399
390
- result = global ? regex . exec ( input ) : null ;
400
+ // Avoid an infinite loop for zero-length matches
401
+ var pos = result . index + ( result [ 0 ] . length || 1 ) ;
402
+
403
+ result = global ? XRegExp . exec ( input , regex , pos ) : null ;
391
404
}
392
405
393
406
}
0 commit comments