The URLPattern API provides a web platform primitive for matching URLs based on a convenient pattern syntax.
The syntax is based on path-to-regexp. Wildcards, named capture groups, regular groups, and group modifiers are all supported.
// Specify the pattern as structured data.
const pattern = new URLPattern({ pathname: "/users/:user" });
const match = pattern.exec("https://blog.example.com/users/joe");
console.log(match.pathname.groups.user); // joe
// Specify a fully qualified string pattern.
const pattern = new URLPattern("https://example.com/books/:id");
console.log(pattern.test("https://example.com/books/123")); // true
console.log(pattern.test("https://deno.land/books/123")); // false
// Specify a relative string pattern with a base URL.
const pattern = new URLPattern("/article/:id", "https://blog.example.com");
console.log(pattern.test("https://blog.example.com/article")); // false
console.log(pattern.test("https://blog.example.com/article/123")); // true
type
{ readonly prototype: URLPattern; new (input: URLPatternInput, baseURL?: string): URLPattern; }