URLPattern.exec - Web documentation
method URLPattern.exec
URLPattern.exec(
baseURL?: string,
): URLPatternResult | null

Match the given input against the stored pattern.

The input can either be provided as an absolute URL string with an optional base, relative URL string with a required base, or as individual components in the form of an URLPatternInit object.

const pattern = new URLPattern("https://example.com/books/:id");

// Match an absolute url string.
let match = pattern.exec("https://example.com/books/123");
console.log(match.pathname.groups.id); // 123

// Match a relative url with a base.
match = pattern.exec("/books/123", "https://example.com");
console.log(match.pathname.groups.id); // 123

// Match an object of url components.
match = pattern.exec({ pathname: "/books/123" });
console.log(match.pathname.groups.id); // 123

Parameters

optional
baseURL: string

Return Type