diff --git a/packages/dgt-utils/lib/router/router.spec.ts b/packages/dgt-utils/lib/router/router.spec.ts index 2a7f7417..89ec6003 100644 --- a/packages/dgt-utils/lib/router/router.spec.ts +++ b/packages/dgt-utils/lib/router/router.spec.ts @@ -122,6 +122,20 @@ describe('Router', () => { }); + it('should not match greedy on /.* routes', async () => { + + delete window.location; + (window.location as any) = new URL('http://localhost/onePart/12345/filler/testing?search=test#test'); + + const testRoute: Route = { + path: '/{{partOne}}/{{numbers}}/.*', + targets: [], + }; + + expect(urlVariables(testRoute).pathParams.get('numbers')).toBe('12345'); + + }); + it('should error when no match was found for every variable', () => { const invalidRoute: Route = { diff --git a/packages/dgt-utils/lib/router/router.ts b/packages/dgt-utils/lib/router/router.ts index 4c1b41c9..ff6760e9 100644 --- a/packages/dgt-utils/lib/router/router.ts +++ b/packages/dgt-utils/lib/router/router.ts @@ -75,7 +75,7 @@ export const urlVariables = (route: Route): UrlVariables => { const searchParams = new URL(window.location.href).searchParams; const hash = new URL(window.location.href).hash; - const regex = new RegExp(`^${route.path.replace(/{{[^/]+}}/ig, '(.+)')}$`, 'i'); + const regex = new RegExp(`^${route.path.replace(/{{[^/]+}}/ig, '(.+?)')}$`, 'i'); const parts = route.path.split('/') .filter((part) => part.startsWith('{{') && part.endsWith('}}'));