TL;DR

The URLPattern API provides a pattern language and a URLPattern interface for matching URLs or specific URL parts and extracting captures. It supports literals, wildcards, named groups, optional and repeated groups, and regex matchers, with normalization and base-URL inheritance rules.

What happened

Mozilla’s documentation describes the URLPattern API, a pattern syntax and URLPattern interface for matching whole URLs or individual URL components. Patterns are built from literal text, wildcards, named capture groups, non‑capturing group delimiters for optional or repeated segments, and explicit regular‑expression groups. The syntax draws from the path-to-regexp library used by some web frameworks; it also includes modifiers (?, +, *) and wildcard shorthand (*) that behave differently depending on URL part. The spec clarifies pathname behavior (pathnames must begin with a slash), anchor handling (start/end anchors are implicit), and several regex caveats such as lookahead/lookbehind pitfalls. Matches are normalized (percent-encoding, punycode, default-port elision, path collapsing) and patterns/test URLs may inherit parts from an optional base URL according to a documented specificity order. The feature is noted as newly available and usable in Web Workers.

Why it matters

  • Provides a declarative way to match and extract parts of URLs without writing manual regex parsing.
  • Built-in normalization reduces mismatches caused by encoding, default ports, or path shortcuts.
  • Group modifiers and named captures make route patterns expressive for routing and request handling.
  • Explicit rules about inheritance and pathname behavior help avoid subtle matching bugs.

Key facts

  • The URLPattern interface represents a pattern that can match full URLs or individual URL parts.
  • Pattern syntax is based on the path-to-regexp library and supports literal strings, wildcards, named groups, non‑capturing group delimiters, and regex groups.
  • A wildcard token (*) is a shorthand for an unnamed capturing group that matches zero or more characters and is greedy.
  • Segment-style groups are non‑greedy by default and obey different delimiters per URL part (e.g., pathname groups don’t match slashes).
  • Regex groups are supported, but some environments may prohibit their use; the hasRegExpGroups property indicates whether regex groups are present.
  • Pathname patterns must include a leading '/', and trailing slashes are not matched automatically unless the pattern allows them.
  • Start (^) and end ($) anchors are redundant because URL parts are implicitly anchored to start and end.
  • Patterns are normalized on parse: Unicode percent-encoding in pathnames, punycode for hostnames, elision of default ports, and path collapse (/foo/./bar/ → /foo/bar).
  • When a base URL is provided, URL parts may be inherited from it based on a specificity order; username and password are never inherited.

What to watch next

  • Browser compatibility details and per-browser behavior differences: not confirmed in the source
  • How major frameworks and routing libraries adopt or map this pattern syntax: not confirmed in the source
  • Any platform restrictions on using regular-expression groups in URLPattern objects (some APIs may prohibit them) — the existence of the hasRegExpGroups flag is documented

Quick glossary

  • URLPattern: An interface that represents a pattern used to match URLs or parts of URLs and extract captured groups.
  • Wildcard (*): A token that acts as an unnamed capturing group matching zero or more characters; it is greedy and matches as much as possible.
  • Named group: A capture group prefixed with a colon (e.g., :id) that extracts a named component from a matched URL.
  • Regular expression group: A group whose matching rules are defined by a regex inside parentheses, allowing constrained or complex matches.
  • Base URL: An optional URL provided when parsing patterns or test URLs whose more-specific parts can be inherited by the pattern or test input.

Reader FAQ

Is URLPattern usable inside Web Workers?
Yes. The documentation notes that the feature is available in Web Workers.

Can URLPattern use regular-expression groups?
Yes, URLPattern supports regex groups, but some APIs may prohibit their use; the hasRegExpGroups property indicates if regex groups are present.

Are trailing slashes in pathnames matched automatically?
No. Trailing slashes are not automatically matched; you must make the slash optional in the pattern to match both forms.

Will username and password be inherited from a base URL?
No. The username and password components are never inherited from a base URL.

Which browsers support the URLPattern API?
not confirmed in the source

URL Pattern API Baseline 2025 NEWLY AVAILABLE Note: This feature is available in Web Workers. The URL Pattern API defines a syntax that is used to create URL pattern matchers….

Sources

Related posts

By

Leave a Reply

Your email address will not be published. Required fields are marked *