Skip to content

Commit 3771813

Browse files
committed
Merge pull request #7 from blakeembrey/express-compat
Improved compatibility
2 parents 626c9a7 + cb988ba commit 3771813

File tree

2 files changed

+407
-35
lines changed

2 files changed

+407
-35
lines changed

index.js

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,19 +32,25 @@ function pathtoRegexp(path, keys, options) {
3232

3333
path = path
3434
.concat(strict ? '' : '/?')
35-
.replace(/\/\(/g, '(?:/')
36-
.replace(/(\/)?(\.)?:(\w+)(?:(\(.*?\)))?(\?)?/g, function (_, slash, format, key, capture, optional) {
37-
keys.push({ name: key, optional: !! optional });
35+
.replace(/\/\(/g, '/(?:')
36+
.replace(/([\/\.])/g, '\\$1')
37+
.replace(/(\\\/)?(\\\.)?:(\w+)(\(.*?\))?(\*)?(\?)?/g, function (match, slash, format, key, capture, star, optional) {
3838
slash = slash || '';
39+
format = format || '';
40+
capture = capture || '([^/' + format + ']+?)';
41+
optional = optional || '';
42+
43+
keys.push({ name: key, optional: !!optional });
44+
3945
return ''
4046
+ (optional ? '' : slash)
4147
+ '(?:'
42-
+ (optional ? slash : '')
43-
+ (format || '') + (capture || (format ? '([^/.]+)' : '([^/]+)')) + ')'
44-
+ (optional || '');
48+
+ format + (optional ? slash : '') + capture
49+
+ (star ? '((?:[\\/' + format + '].+?)?)' : '')
50+
+ ')'
51+
+ optional;
4552
})
46-
.replace(/([\/.])/g, '\\$1')
4753
.replace(/\*/g, '(.*)');
4854

49-
return new RegExp('^' + path + (end ? '$' : ''), sensitive ? '' : 'i');
55+
return new RegExp('^' + path + (end ? '$' : '(?=\/|$)'), sensitive ? '' : 'i');
5056
};

0 commit comments

Comments
 (0)