Skip to content

Commit c4272e4

Browse files
authoredAug 22, 2024
Allow a non-lookahead regex (#312)
1 parent 51a1955 commit c4272e4

File tree

2 files changed

+71
-1
lines changed

2 files changed

+71
-1
lines changed
 

‎index.js

+6-1
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ function pathtoRegexp(path, keys, options) {
3131
var strict = options.strict;
3232
var end = options.end !== false;
3333
var flags = options.sensitive ? '' : 'i';
34+
var lookahead = options.lookahead !== false;
3435
var extraOffset = 0;
3536
var keysOffset = keys.length;
3637
var i = 0;
@@ -123,7 +124,11 @@ function pathtoRegexp(path, keys, options) {
123124
}
124125

125126
// If the path is non-ending, match until the end or a slash.
126-
path += (end ? '$' : (path[path.length - 1] === '/' ? '' : '(?=\\/|$)'));
127+
if (end) {
128+
path += '$';
129+
} else if (path[path.length - 1] !== '/') {
130+
path += lookahead ? '(?=\\/|$)' : '(?:\/|$)';
131+
}
127132

128133
return new RegExp(path, flags);
129134
};

‎test.js

+65
Original file line numberDiff line numberDiff line change
@@ -527,6 +527,19 @@ describe('path-to-regexp', function () {
527527
assert.equal(m[1], 'test');
528528
});
529529

530+
it('should do non-ending matches (no lookahead)', function () {
531+
var params = [];
532+
var m = pathToRegExp('/:test', params, { end: false, lookahead: false }).exec('/test/route');
533+
534+
assert.equal(params.length, 1);
535+
assert.equal(params[0].name, 'test');
536+
assert.equal(params[0].optional, false);
537+
538+
assert.equal(m.length, 2);
539+
assert.equal(m[0], '/test/');
540+
assert.equal(m[1], 'test');
541+
});
542+
530543
it('should match trailing slashes in non-ending non-strict mode', function () {
531544
var params = [];
532545
var re = pathToRegExp('/:test', params, { end: false });
@@ -571,6 +584,34 @@ describe('path-to-regexp', function () {
571584
assert.equal(m[0], '/route/');
572585
});
573586

587+
it('should match trailing slashes in non-ending non-strict mode (no lookahead)', function () {
588+
var params = [];
589+
var re = pathToRegExp('/route/', params, { end: false, lookahead: false });
590+
var m;
591+
592+
assert.equal(params.length, 0);
593+
594+
m = re.exec('/route/');
595+
596+
assert.equal(m.length, 1);
597+
assert.equal(m[0], '/route/');
598+
599+
m = re.exec('/route/test');
600+
601+
assert.equal(m.length, 1);
602+
assert.equal(m[0], '/route/');
603+
604+
m = re.exec('/route');
605+
606+
assert.equal(m.length, 1);
607+
assert.equal(m[0], '/route');
608+
609+
m = re.exec('/route//');
610+
611+
assert.equal(m.length, 1);
612+
assert.equal(m[0], '/route//');
613+
});
614+
574615
it('should match trailing slashing in non-ending strict mode', function () {
575616
var params = [];
576617
var re = pathToRegExp('/route/', params, { end: false, strict: true });
@@ -616,6 +657,24 @@ describe('path-to-regexp', function () {
616657
assert.equal(m[0], '/route');
617658
});
618659

660+
it('should not match trailing slashes in non-ending strict mode (no lookahead)', function () {
661+
var params = [];
662+
var re = pathToRegExp('/route', params, { end: false, strict: true, lookahead: false });
663+
var m;
664+
665+
assert.equal(params.length, 0);
666+
667+
m = re.exec('/route');
668+
669+
assert.equal(m.length, 1);
670+
assert.equal(m[0], '/route');
671+
672+
m = re.exec('/route/');
673+
674+
assert.ok(m.length, 1);
675+
assert.equal(m[0], '/route/');
676+
});
677+
619678
it('should match text after an express param', function () {
620679
var params = [];
621680
var re = pathToRegExp('/(:test)route', params);
@@ -728,6 +787,12 @@ describe('path-to-regexp', function () {
728787
});
729788

730789
it('should pull out matching named groups', function () {
790+
const majorVersion = Number(process.version.split('.')[0].replace('v', ''));
791+
if (majorVersion < 10) {
792+
console.log('skipping test: node <10 does not support named capture groups');
793+
return;
794+
}
795+
731796
var params = [];
732797
var re = pathToRegExp(/\/(.*)\/(?<foo>.*)\/(.*)/, params);
733798
var m;

0 commit comments

Comments
 (0)
Please sign in to comment.