The exec()
method of the URLPattern
interface takes a URL or object of URL parts, and returns either an object containing the results of matching the URL to the pattern, or null
if the URL does not match the pattern.
exec(input)
exec(input, baseURL)
An object
with an inputs
key containing the array of arguments passed into the function, and keys for each of the URL parts containing the matched input, and matched groups for that part.
This example shows how to use the exec()
method to match a URL against a pattern. The example prints the result of the exec()
calls to the console.
const pattern = new URLPattern("http{s}?://*.example.com/books/:id");
console.log(pattern.exec("https://example.com/books/123"));
let match = pattern.exec("https://store.example.com/books/123");
console.log(match.inputs);
console.log(match.protocol);
console.log(match.username);
console.log(match.password);
console.log(match.hostname);
console.log(match.port);
console.log(match.pathname);
console.log(match.search);
console.log(match.hash);
pattern.exec("/books/123", "http://store.example.com");
pattern.exec("/books/123", "data:text/plain,hello world!");
pattern.exec("/books/123");
pattern.exec({
pathname: "/books/123",
baseURL: "http://store.example.com",
});
pattern.exec({
protocol: "https",
hostname: "store.example.com",
pathname: "/books/123",
});
pattern.exec({
protocol: "file",
hostname: "store.example.com",
pathname: "/books/123",
});