https://developer.mozilla.org/en-US/docs/Web/API/URL_Pattern_API * Skip to main content * Skip to search HTML HTML: Markup language HTML reference + Elements + Global attributes + Attributes + See all... HTML guides + Responsive images + HTML cheatsheet + Date & time formats + See all... Markup languages + SVG + MathML + XML CSS CSS: Styling language CSS reference + Properties + Selectors + At-rules + Values + See all... CSS guides + Box model + Animations + Flexbox + Colors + See all... Layout cookbook + Column layouts + Centering an element + Card component + See all... JavaScriptJS JavaScript: Scripting language JS reference + Standard built-in objects + Expressions & operators + Statements & declarations + Functions + See all... JS guides + Control flow & error handing + Loops and iteration + Working with objects + Using classes + See all... Web APIs Web APIs: Programming interfaces Web API reference + File system API + Fetch API + Geolocation API + HTML DOM API + Push API + Service worker API + See all... Web API guides + Using the Web animation API + Using the Fetch API + Working with the History API + Using the Web speech API + Using web workers All All web technology Technologies + Accessibility + HTTP + URI + Web extensions + WebAssembly + WebDriver + See all... Topics + Media + Performance + Privacy + Security + Progressive web apps Learn Learn web development Frontend developer course + Getting started modules + Core modules + MDN Curriculum Learn HTML + Structuring content with HTML module Learn CSS + CSS styling basics module + CSS layout module Learn JavaScript + Dynamic scripting with JavaScript module Tools Discover our tools * Playground * HTTP Observatory * Border-image generator * Border-radius generator * Box-shadow generator * Color format converter * Color mixer * Shape generator About Get to know MDN better * About MDN * Advertise with us * Community * MDN on GitHub Blog Toggle sidebar 1. Web 2. Web APIs 3. URL Pattern API Theme * OS default * Light * Dark English (US) [ ] Remember language Learn more * Deutsch * English (US) * Ri Ben Yu URL Pattern API Baseline 2025 Newly available Since [?]September 2025[?], this feature works across the latest devices and browser versions. This feature might not work in older devices or browsers. * Learn more * See full compatibility * Report feedback Note: This feature is available in Web Workers. The URL Pattern API defines a syntax that is used to create URL pattern matchers. These patterns can be matched against URLs or individual URL components. In this article * Concepts and usage * Interfaces * Pattern syntax * Inheritance from a base URL * Case sensitivity * Examples * Specifications * Browser compatibility * See also Concepts and usage Patterns are specified using the URLPattern interface. The pattern syntax is based on the syntax from the path-to-regexp library. Patterns can contain: * Literal strings which will be matched exactly. * Wildcards (/posts/*) that match any character. * Named groups (/books/:id) which extract a part of the matched URL. * Non-capturing groups (/books{/old}?) which make parts of a pattern optional or be matched multiple times. * RegExp groups (/books/(\\d+)) which make arbitrarily complex regex matches. Note that the parentheses are not part of the regex but instead define their contents as a regex. Some APIs prohibit the use of regular expression groups in URLPattern objects. The hasRegExpGroups property indicates whether or not regular expression groups are used. You can find details about the syntax in the pattern syntax section below. Interfaces URLPattern Represents a pattern that can match URLs or parts of URLs. The pattern can contain capturing groups that extract parts of the matched URL. Pattern syntax The syntax for patterns is based on the path-to-regexp JavaScript library. This syntax is similar to the one used in Ruby on Rails, or JavaScript frameworks like Express or Next.js. Fixed text and capture groups Each pattern can contain a combination of fixed text and groups. The fixed text is a sequence of characters that is matched exactly. Groups match an arbitrary string based on matching rules. Each URL part has its own default rules that are explained below, but they can be overwritten. js // A pattern matching some fixed text const pattern = new URLPattern({ pathname: "/books" }); console.log(pattern.test("https://example.com/books")); // true console.log(pattern.exec("https://example.com/books").pathname.groups); // {} js // A pattern matching with a named group 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' } Segment wildcard By default, a group matching the pathname part of the URL will match all characters but the forward slash (/). In the hostname part, the group will match all characters except the dot (.). In all other parts, the group will match all characters. The segment wildcard is non-greedy, meaning that it will match the shortest possible string. Regex matchers Instead of using the default match rules for a group, you can specify a regex for each group by specifying it in parentheses. This regex defines the matching rules for the group. Below is an example of a regex matcher on a named group that constrains the group to only match if it contains one or more digits: js const pattern1 = new URLPattern("/books/:id(\\d+)", "https://example.com"); console.log(pattern1.test("https://example.com/books/123")); // true console.log(pattern1.test("https://example.com/books/abc")); // false console.log(pattern1.test("https://example.com/books/")); // false You can also use regex when constructing a URLPattern with the object syntax. js const pattern2 = new URLPattern({ pathname: "/books/:id(\\d+)" }); console.log(pattern2.test("https://example.com/books/123")); // true console.log(pattern2.test("https://example.com/books/abc")); // false console.log(pattern2.test("https://example.com/books/")); // false Pathname matching The pathname URL-part always starts with /. If you omit the / in your regular expression the match will fail. The example below js // Doesn't match, because omits the `/` const pattern1 = new URLPattern({ pathname: "(b.*)" }); console.log(pattern1.test("https://example.com/b")); // false console.log(pattern1.test("https://example.com/ba")); // false The following examples include the /: js // Matches URL where path is exactly "/b" const pattern2 = new URLPattern({ pathname: "(/b)" }); console.log(pattern2.test("https://example.com/b")); // true console.log(pattern2.test("https://example.com/ba")); // false // Matches URL where path is /b followed by any number of characters const pattern3 = new URLPattern({ pathname: "(/b.*)" }); console.log(pattern3.test("https://example.com/b")); // true console.log(pattern3.test("https://example.com/ba")); // true Start and end of line anchors The start of line anchor (^) and end-of line anchor ($) are used to anchor patterns to the start and end of the test string, respectively. While these can be specified for the start and end of a URL-part they are redundant. This is because all URL-parts are implicitly preceded by the ^ anchor, and followed by the $ anchor. The following code demonstrates that it doesn't matter whether or not ^ is specified. The example uses a pattern in the protocol URL-part, but the other parts of the URL behave the same. js // with `^` in protocol const pattern1 = new URLPattern({ protocol: "(^https?)" }); console.log(pattern1.test("https://example.com/index.html")); // true // without `^` in protocol const pattern2 = new URLPattern({ protocol: "(https?)" }); console.log(pattern2.test("https://example.com/index.html")); // true The code below demonstrates that it doesn't matter whether or not $ is specified. js // with `$` in pathname const pattern1 = new URLPattern({ pathname: "(/path$)" }); console.log(pattern1.test("https://example.com/path")); // true // without `$` in pathname const pattern2 = new URLPattern({ pathname: "(/path)" }); console.log(pattern2.test("https://example.com/path")); // true // with `$` in hash const pattern3 = new URLPattern({ hash: "(/hash$)" }); console.log(pattern3.test("https://example.com/#hash")); // true // without `$` in hash const pattern4 = new URLPattern({ hash: "(/hash)" }); console.log(pattern4.test("https://example.com/#hash")); // true Lookahead and lookbehind assertions Lookahead and lookbehind asserts allow you to specify that text ahead or behind the current parsing position matches a particular pattern, without that match being captured, or the characters being consumed. There are four types of assertions: * (?=...): A positive lookahead assertion specifies a pattern that the following characters must match. * (?!...): A negative lookahead assertion specifies a pattern that the following characters must not match. * (?<=...): A positive lookbehind assertion specifies a pattern that the preceding characters must match. * (? const pattern2 = new URLPattern({ pathname: "(/a(?!b).*)" }); console.log(pattern2.test("https://example.com/ab")); // false console.log(pattern2.test("https://example.com/ax")); // true The following example shows a positive lookbehind match that matches on a pathname like /ba. The pattern matches /, then . to consume the next character, followed by the assertion that the previous character was a b, and then an a. js // positive-lookbehind const pattern = new URLPattern({ pathname: "(/.(?<=b)a)" }); console.log(pattern.test("https://example.com/ba")); // true console.log(pattern.test("https://example.com/xa")); // false This example shows a negative lookbehind match that matches on a pathname like /a. The pattern matches /, then . to consume the next character (x), followed by the assertion that the previous character was not b, and then an a. js // negative-lookbehind const pattern4 = new URLPattern({ pathname: "(/.*(?