whatwg / urlpattern

URL Pattern Standard
https://urlpattern.spec.whatwg.org/
Other
154 stars 20 forks source link

Custom named full-wildcard #197

Closed bdc34 closed 10 months ago

bdc34 commented 10 months ago

What problem are you trying to solve?

I have a use case where I would like to match custom named for full-wildcard. It might look like this in urlpattern:

"https://example.com/image_from_tar/tarfile:/tarpath:*"

Where I would expect

https://exampl.ecom/image_from_tar/2301.12345/figures/old/figure2a.png

to result in a match and :tarfile to be 2301.12345 and tarpath: to be /figures/old/figure2a.png or something similar.

What solutions exist today?

No response

How would you solve it?

No response

Anything else?

From section https://urlpattern.spec.whatwg.org/#example-pattern-strings it seems pattern string capture groups are supported but this use case is not as I understand by the text "up to a component-specific separator (/ in the pathname"

Also the parsing step 14 mentioned a custom name for a segment_wildcard but no custom name is mentioned for full-wildcard. This makes me think that a custom name is not possible on a full-wildcard in the current version.

I was looking at 24 Oct 2023 text of https://urlpattern.spec.whatwg.org/#part

Thanks.

sisidovski commented 10 months ago

Thank you for filing the issue.

IIUC you'd like to capture matched strings with named groups. The named group should have : prefix, not suffix. If you have named groups with prefix. The result would be about your expectation.

const pattern = new URLPattern({pathname: "/image_from_tar/:tarfile/:tarpath*"})
const result = pattern.exec('https://exampl.ecom/image_from_tar/2301.12345/figures/old/figure2a.png')
result.pathname.groups
=> {tarfile: '2301.12345', tarpath: 'figures/old/figure2a.png'}
jeremyroman commented 10 months ago

It seems like Shunya's answer likely resolves this issue; reopen if not.