Get to know MDN better
Since September 2025, this feature works across the latest devices and browser versions. This feature might not work in older devices or browsers.
Note: This feature is available in Web Workers.
The URLPattern() constructor returns a new URLPattern object representing the URLs that will be matched by this pattern.
An object that has separate properties for defining the patterns used to match each part of a URL.
The object members can be any (or none) of:
protocol OptionalA pattern that matches a URL protocol, such as http, https, or "http{s}?" (to match both https and http).
username OptionalA pattern that matches a URL username.
password OptionalA pattern that matches a URL password.
hostname OptionalA pattern that matches a URL hostname.
port OptionalA pattern that matches a URL port.
pathname OptionalA pattern that matches a URL pathname.
search OptionalA pattern that matches a URL search.
hash OptionalA pattern that matches a URL hash.
baseURL OptionalA string that provides an absolute URL from which undefined less-specific object properties may be inherited.
url OptionalA string representing URL patterns to match.
This is formatted as an absolute or relative URL but may contain markup to indicate matching patterns and escape sequences. If formatted as a relative URL, then baseURL must also be provided.
baseURL OptionalA string that provides an absolute URL from which undefined less-specific URL-parts may be inherited This must be set when url is a relative URL, and must not be set if input is used (input.baseURL may be used to provide inherited values for an input, but, unlike this property, is never required).
options OptionalAn object providing options for matching the given pattern. The allowed object members are:
ignoreCase OptionalEnables case-insensitive matching if set to true. If omitted or set to false, matching will be case-sensitive.
Note: All the URL parts in the input properties and the url are optional. If not specified in those parameters, some values may be inherited from the baseURL, depending on what other URL-parts are defined. Omitted parts are normalized to wildcards (*).
Indicates one of the following:
The URLPattern constructor can take either an "input" object or a URL string and optional baseURL. Both forms can also take an options object argument that sets additional matching options, such as case sensitivity.
The input object used in the first type of constructor describes the URLs that should be matched by specifying patterns for individual URL parts: protocol, username, password, hostname, port, pathname, search, hash, and baseURL. If the baseURL property is provided it will be parsed as a URL and may be used to populate any other properties that are missing (see the following section Inheritance from a base URL). Properties that are omitted or not filled by the baseURL property default to the wildcard string (*), which match against any corresponding value in a URL.
The second type of constructor takes a URL string that may contain patterns embedded in it. The string may specify an absolute or relative URL — if the pattern is relative, then baseURL must be provided as the second argument. Note that it may be necessary to escape some characters in the URL string if it is ambiguous whether the character is separating different URL components or is part of a pattern.
URL-parts that are more specific than the least-specific part defined in the url may be inherited from baseURL (or from input.baseURL for input). Intuitively this means that if the pathname part is specified in the input, the parts to its left in a URL may be inherited from the base URL (protocol, hostname and port), while the parts to its right may not (search and hash). The username and password are never inherited from the base URL.
For more information see Inheritance from a BaseURL in the API overview.
Unlike other URL parts, the port may be implicitly set if you specify an url or base URL (either in the baseURL parameter or in the object) and don't explicitly specify a port. In this case the port will be set to the empty string ("") and match the default port (443).
For example, these patterns all set the port pattern to "":
If you don't specify the hostname in an url or baseURL, the port will default to the wildcard string (*):
The pattern syntax includes a number of characters that can occur naturally in URLs, such as:
If you're constructing a URLPattern using the url string parameter these special characters are assumed to be part of the pattern syntax (if there is any ambiguity). If you are using the characters to represent parts of the URL then you will need to escape them, by preceding the characters with \\ (or avoid the problem by constructing URLPattern using the object syntax).
For example, the following pattern escapes the ? character, which makes this pattern match a search URL-part of "fred"
Similarly, the Match the username and password example below shows a case where the : separator needs to be escaped.
This code demonstrates that URL-parts that are not supplied in a URL or inherited from a base URL default to the wildcard value.
This sets the username and password URL parts using the pattern string. Note how the : separator needs to be escaped when using the pattern string. Without this the username pattern would be myusername:mypassword.
For this reason it is often more natural (and safer) to use the object syntax.
Setting the ignoreCase option to true in the constructor switches all matching operations to case-insensitive for the given pattern:
This provides a real world example of inheritance. The pathname is explicitly specified. The values that are less specific than the pathname, such as the protocol and hostname are inherited. The more specific values are ignored, and default to their default values (such as "*" for the search and hash, and "" for the port).
| URL Pattern # dom-urlpattern-urlpattern |
Enable JavaScript to view this browser compatibility table.
This page was last modified on Oct 30, 2025 by MDN contributors.
Your blueprint for a better internet.
Visit Mozilla Corporation’s not-for-profit parent, the Mozilla Foundation.
Portions of this content are ©1998–2026 by individual mozilla.org contributors. Content available under a Creative Commons license.