URLPattern

open class URLPattern(input: String = definedExternally, options: URLPatternOptions = definedExternally)(source)

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("https://blog.example.com/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/:id", "https://blog.example.com");
console.log(pattern.test("https://blog.example.com/article")); // false
console.log(pattern.test("https://blog.example.com/article/123")); // true

MDN Reference

Constructors

Link copied to clipboard
constructor(input: URLPatternInit, options: URLPatternOptions = definedExternally)
constructor(input: String, baseURL: String, options: URLPatternOptions = definedExternally)
constructor(input: URLPatternInit, baseURL: String, options: URLPatternOptions = definedExternally)
constructor(input: String = definedExternally, options: URLPatternOptions = definedExternally)

Properties

Link copied to clipboard

The pattern string for the hash.

Link copied to clipboard

Whether or not any of the specified groups use regexp groups.

Link copied to clipboard

The pattern string for the hostname.

Link copied to clipboard

The pattern string for the password.

Link copied to clipboard

The pattern string for the pathname.

Link copied to clipboard

The pattern string for the port.

Link copied to clipboard

The pattern string for the protocol.

Link copied to clipboard

The pattern string for the search.

Link copied to clipboard

The pattern string for the username.

Functions

Link copied to clipboard
fun exec(input: String, baseURL: String = definedExternally): URLPatternResult?

Match the given input against the stored pattern.

fun exec(input: URLPatternInit, baseURL: String = definedExternally): URLPatternResult?
Link copied to clipboard
fun test(input: String, baseURL: String = definedExternally): Boolean

Test if the given input matches the stored pattern.

fun test(input: URLPatternInit, baseURL: String = definedExternally): Boolean