Skip to main content
method URLPattern.prototype.test

Test if the given input matches 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");

// Test a url string.
console.log(pattern.test("https://example.com/books/123")); // true

// Test a relative url with a base.
console.log(pattern.test("/books/123", "https://example.com")); // true

// Test an object of url components.
console.log(pattern.test({ pathname: "/books/123" })); // true

Parameters

optional
baseURL: string

Returns

boolean