Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ module.exports = pathtoRegexp;
* Match matching groups in a regular expression.
*/
var MATCHING_GROUP_REGEXP = /\((?!\?)/g;
var MATCHING_NAMED_GROUP_REGEXP = /\(\?<(.+?)>/g;

/**
* Normalize the given path string,
Expand Down Expand Up @@ -45,6 +46,13 @@ function pathtoRegexp(path, keys, options) {
offset: m.index
});
}
while (m = MATCHING_NAMED_GROUP_REGEXP.exec(path.source)) {
keys.push({
name: m[1],
optional: false,
offset: m.index
});
}

return path;
}
Expand Down
25 changes: 25 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -574,6 +574,7 @@ describe('path-to-regexp', function () {
it('should match trailing slashing in non-ending strict mode', function () {
var params = [];
var re = pathToRegExp('/route/', params, { end: false, strict: true });
var m;

assert.equal(params.length, 0);

Expand All @@ -600,6 +601,7 @@ describe('path-to-regexp', function () {
it('should not match trailing slashes in non-ending strict mode', function () {
var params = [];
var re = pathToRegExp('/route', params, { end: false, strict: true });
var m;

assert.equal(params.length, 0);

Expand All @@ -617,6 +619,7 @@ describe('path-to-regexp', function () {
it('should match text after an express param', function () {
var params = [];
var re = pathToRegExp('/(:test)route', params);
var m;

assert.equal(params.length, 1);
assert.equal(params[0].name, 'test');
Expand Down Expand Up @@ -723,6 +726,28 @@ describe('path-to-regexp', function () {
assert.equal(m[0], '/route');
assert.equal(m[1], '/route');
});

it('should pull out matching named groups', function () {
var params = [];
var re = pathToRegExp(/\/(.*)\/(?<foo>.*)\/(.*)/, params);
var m;

assert.equal(params.length, 3);
assert.equal(params[0].name, 0);
assert.equal(params[0].optional, false);
assert.equal(params[1].name, 1);
assert.equal(params[1].optional, false);
assert.equal(params[2].name, 'foo');
assert.equal(params[2].optional, false);

m = re.exec('/foo/bar/baz');

assert.equal(m.length, 4);
assert.equal(m[0], '/foo/bar/baz');
assert.equal(m[1], 'foo');
assert.equal(m[2], 'bar');
assert.equal(m[3], 'baz');
})
});

describe('arrays', function () {
Expand Down