From 43e4443a27d0c5df114f5370eda62f799faa8366 Mon Sep 17 00:00:00 2001 From: Norbert Szydlik Date: Fri, 23 Feb 2024 06:11:00 +0100 Subject: [PATCH 1/2] Add support for named matching groups --- index.js | 8 ++++++++ test.js | 25 +++++++++++++++++++++++++ 2 files changed, 33 insertions(+) diff --git a/index.js b/index.js index 500d1da..c5fc496 100644 --- a/index.js +++ b/index.js @@ -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, @@ -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; } diff --git a/test.js b/test.js index 28fce0a..a449c5a 100644 --- a/test.js +++ b/test.js @@ -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); @@ -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); @@ -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'); @@ -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(/\/(.*)\/(?.*)\/(.*)/, 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 () { From 19d395db608c8552272d59e0a9f07ed2e5312615 Mon Sep 17 00:00:00 2001 From: Norbert Szydlik Date: Thu, 21 Mar 2024 12:07:25 +0100 Subject: [PATCH 2/2] Combine regex for named and unnamed groups --- index.js | 12 ++---------- test.js | 4 ++-- 2 files changed, 4 insertions(+), 12 deletions(-) diff --git a/index.js b/index.js index c5fc496..7ad98c6 100644 --- a/index.js +++ b/index.js @@ -7,8 +7,7 @@ module.exports = pathtoRegexp; /** * Match matching groups in a regular expression. */ -var MATCHING_GROUP_REGEXP = /\((?!\?)/g; -var MATCHING_NAMED_GROUP_REGEXP = /\(\?<(.+?)>/g; +var MATCHING_GROUP_REGEXP = /\((?:\?<(.*?)>)?(?!\?)/g; /** * Normalize the given path string, @@ -41,14 +40,7 @@ function pathtoRegexp(path, keys, options) { if (path instanceof RegExp) { while (m = MATCHING_GROUP_REGEXP.exec(path.source)) { keys.push({ - name: name++, - optional: false, - offset: m.index - }); - } - while (m = MATCHING_NAMED_GROUP_REGEXP.exec(path.source)) { - keys.push({ - name: m[1], + name: m[1] || name++, optional: false, offset: m.index }); diff --git a/test.js b/test.js index a449c5a..412cff9 100644 --- a/test.js +++ b/test.js @@ -735,9 +735,9 @@ describe('path-to-regexp', function () { 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].name, 'foo'); assert.equal(params[1].optional, false); - assert.equal(params[2].name, 'foo'); + assert.equal(params[2].name, 1); assert.equal(params[2].optional, false); m = re.exec('/foo/bar/baz');