path-to-regexp icon indicating copy to clipboard operation
path-to-regexp copied to clipboard

Multiple parameters suffixed with an asterisk does not match - works with v5/v3

Open blikblum opened this issue 6 years ago • 7 comments

The following pattern used to match:

let re = pathToRegexp("/files/:path*\\.:ext*", paramNames);

let match = "/files/my/photo.jpg/gif".match(re);

Version 3.0 works: https://codesandbox.io/s/old-thunder-x209j3nkko Version 5.0 works: https://codesandbox.io/s/angry-bhaskara-gkzf3 Version 6.1 does not work: https://codesandbox.io/s/naughty-cherry-lgsiz

blikblum avatar Dec 16 '19 10:12 blikblum

This is expected behaviour, see https://github.com/pillarjs/path-to-regexp/releases/tag/v6.0.0. Version 3 was the first version to introduce this piece of magic, but for 6.0 stabilizing I wanted to remove things that cause confusion and this was a big one.

blakeembrey avatar Dec 16 '19 17:12 blakeembrey

According to https://github.com/pillarjs/path-to-regexp/releases/tag/v6.0.0 the v6 should be compatible with v2 but unfortunately is not the case.

In v2, without the period escape the following would match path and ext See https://codesandbox.io/s/frosty-leftpad-x75l707pxw

let paramNames = [];
let re = pathToRegexp("/files/:path*.:ext*", paramNames);

let match = "/files/my/photo.jpg/gif".match(re);

But in v6 only path is matched: See https://codesandbox.io/s/path-to-regexp-61-multiple-splat-no-escape-5k2b4

Notice that the period escape was originally suggested in this issue comment.

blikblum avatar Dec 17 '19 00:12 blikblum

Ah, thanks. I see the issue. When you escape the previous character now, it ends up without a prefix or suffix effectively making it impossible to actually match a repeated group. We could default to / (configurable) as the prefix to support the existing use-case.

blakeembrey avatar Dec 17 '19 01:12 blakeembrey

Is there a way to support multiple splat with current features, e.g., adding some more notation?

I don't mind if i need to change the pattern.

blikblum avatar Dec 17 '19 09:12 blikblum

@blakeembrey should this be labeled as a valid bug that needs a fix?

mastermatt avatar Jun 06 '20 21:06 mastermatt

Sure. It should be pretty easy to fix, but it'd be good to come up with possible notation too.

blakeembrey avatar Jun 06 '20 22:06 blakeembrey

This would be fixed by https://github.com/pillarjs/path-to-regexp/pull/270!

HansBrende avatar Feb 02 '22 02:02 HansBrende