Web Design

URL Pattern API

A web standard for matching URLs using pattern syntax

URL Pattern API

The URL Pattern API is a web standard for matching URLs against patterns. The syntax is based on the popular path-to-regexp library used by Express, Ruby on Rails, and Next.js.

Key Features

  • Literal strings — matched exactly
  • Wildcards (/posts/*) — match any character
  • Named groups (/books/:id) — extract parts of the matched URL
  • Non-capturing groups (/books{/old}?) — optional or repeated parts
  • RegExp groups (/books/(\\d+)) — complex regex matching

Example

const pattern = new URLPattern({ pathname: "/books/:id" });

console.log(pattern.test("https://example.com/books/123")); // true
console.log(pattern.exec("https://example.com/books/123").pathname.groups);
// { id: "123" }
MDN — URL Pattern API documentation
Original reference tweet from Mozilla Developer Network