URL Pattern API

New web tool splits the crowd: handy matcher or weird object

TLDR: Browsers now offer a URL Pattern API to match and extract parts of web addresses, noted as available in Web Workers. The community is split: fans say it’s a helpful tool, skeptics fear misuse as a router and complain about moving from simple strings to complex objects, with confusion over where it runs.

The web just got a new trick: the URL Pattern API, a way to test and extract parts of web addresses like “/books/:id” or “/posts/*”. Think of it as pattern-matching for links, inspired by popular routing libraries. It supports named groups, optional segments, and even regex (regular expressions) for strict matches. A note says it’s available in Web Workers—browser background scripts—so developers can process URLs off the main page.

Commenters rolled in fast. One camp cheered: “Great tool. So glad we have something!” while warning it could be misused as a full-on router. User jauntywundrkind called the addition “controversial” because people would try to linearly parse URLs with it—cue pearl-clutching and sarcasm. The HN thread turned into a referendum on how much power the browser should hand out versus letting apps keep control.

Then came confusion: petesergeant asked if “available in Web Workers” means also available there or only there. Cue memes about features “working only under a full moon.” Meanwhile, socketcluster went classic: “I don’t like this API,” lamenting the move from the universal URL string to a “weird object.” It’s Team String vs Team Pattern, with everyone debating whether regex is genius or headache.

Key Points

  • The URL Pattern API uses the URLPattern interface to match full URLs or specific components.
  • Pattern syntax is based on path-to-regexp and supports literals, wildcards, named groups, optional segments, and regex groups.
  • Default match rules vary by URL part: pathname excludes '/', hostname excludes '.', others match all characters; wildcards are non-greedy.
  • Some APIs disallow regex groups; the hasRegExpGroups property reports if a pattern uses them.
  • Pathname patterns must include a leading '/', and examples show using pattern.test and pattern.exec to validate and extract groups.

Hottest takes

"Great tool. So glad we have something!" — jauntywundrkind
"... is _also_ available in Web Workers, or _only_ available in Web Workers?" — petesergeant
"Overall I dislike the shift away from a URL as a language-agnostic string primitive" — socketcluster
Made with <3 by @siedrix and @shesho from CDMX. Powered by Forge&Hive.