Skip to main content
method URLPattern.prototype.exec

Match the given input against the stored pattern.

The input can either be provided as a url string (with an optional base), or as individual components in the form of an object.

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

// Match a 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