whatwg / urlpattern

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

Hash test matching different from path matching #192

Closed jvailius closed 1 year ago

jvailius commented 1 year ago

Hi, does someone know if there a particular reason for this behavior: The path is (correctly I guess) not matched but the hash is.

const pattern = new URLPattern("/#/books/:id", "http://example.com");
pattern.test("http://example.com/#/books/1/page/0") // true
const pattern = new URLPattern("/books/:id", "http://example.com");
pattern.test("http://example.com/books/1/page/0") // false

Thank you.

Tested with Google Chrome 117.0.5938.149 (Official Build) (64-bit)

jeremyroman commented 1 year ago

/ has special treatment as a separating character in the pathname component, but not in the hash (where / has special meaning only for libraries that choose to treat it that way).

As one workaround, you can use a regex group, like so:

const pattern = new URLPattern('/#/books/:id([^/]+)', 'http://example.com');
pattern.test("http://example.com/#/books/1/page/0") // false