URLPattern

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("/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", "https://blog.example.com");
console.log(pattern.test("https://blog.example.com/article")); // true
console.log(pattern.test("https://blog.example.com/article/123")); // false
class URLPattern {
constructor(input: URLPatternInput, baseURL?: string);
readonly hash: string;
readonly hostname: string;
readonly password: string;
readonly pathname: string;
readonly port: string;
readonly protocol: string;
readonly search: string;
readonly username: string;
 
exec(input: URLPatternInput, baseURL?: string): URLPatternResult | null;
test(input: URLPatternInput, baseURL?: string): boolean;
}

§Constructors

§
new URLPattern(input: URLPatternInput, baseURL?: string)
[src]

§Properties

§
hash: string
[src]

The pattern string for the hash.

§
hostname: string
[src]

The pattern string for the hostname.

§
password: string
[src]

The pattern string for the password.

§
pathname: string
[src]

The pattern string for the pathname.

§
port: string
[src]

The pattern string for the port.

§
protocol: string
[src]

The pattern string for the protocol.

§
username: string
[src]

The pattern string for the username.

§Methods

§
exec(input: URLPatternInput, baseURL?: string): URLPatternResult | null
[src]

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
§
test(input: URLPatternInput, baseURL?: string): boolean
[src]

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