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. The URL Pattern API is used by the URLPattern
interface.
Concepts and usage
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 with a few limitations.
You can find details about the syntax in the pattern syntax section below.
URL Pattern API interfaces
The URL Pattern API only has a single related interface:
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.
const pattern = new URLPattern({ pathname: "/books" });
console.log(pattern.test("https://example.com/books"));
console.log(pattern.exec("https://example.com/books").pathname.groups);
const pattern = new URLPattern({ pathname: "/books/:id" });
console.log(pattern.test("https://example.com/books/123"));
console.log(pattern.exec("https://example.com/books/123").pathname.groups);
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 use a regex for each group. 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:
const pattern = new URLPattern("/books/:id(\\d+)", "https://example.com");
console.log(pattern.test("https://example.com/books/123"));
console.log(pattern.test("https://example.com/books/abc"));
console.log(pattern.test("https://example.com/books/"));
Regex matchers limitations
Some regex patterns do not work as you may expect:
- Starts with
^
will only match if used at the start of the protocol portion of the URLPattern and is redundant if used.
const pattern = new URLPattern({ pathname: "(^b)" });
console.log(pattern.test("https://example.com/ba"));
console.log(pattern.test("https://example.com/xa"));
const pattern = new URLPattern({ protocol: "(^https?)" });
console.log(pattern.test("https://example.com/index.html"));
console.log(pattern.test("xhttps://example.com/index.html"));
const pattern = new URLPattern({ protocol: "(https?)" });
console.log(pattern.test("https://example.com/index.html"));
console.log(pattern.test("xhttps://example.com/index.html"));
- Ends with
$
will only match if used at the end of the hash portion of the URLPattern and is redundant if used.
const pattern = new URLPattern({ pathname: "(path$)" });
console.log(pattern.test("https://example.com/path"));
console.log(pattern.test("https://example.com/other"));
const pattern = new URLPattern({ hash: "(hash$)" });
console.log(pattern.test("https://example.com/#hash"));
console.log(pattern.test("xhttps://example.com/#otherhash"));
const pattern = new URLPattern({ hash: "(hash)" });
console.log(pattern.test("https://example.com/#hash"));
console.log(pattern.test("xhttps://example.com/#otherhash"));
- Lookaheads, and lookbehinds will never match any portion of the URLPattern.
const pattern = new URLPattern({ pathname: "(a(?=b))" });
console.log(pattern.test("https://example.com/ab"));
console.log(pattern.test("https://example.com/ax"));
const pattern = new URLPattern({ pathname: "(a(?!b))" });
console.log(pattern.test("https://example.com/ab"));
console.log(pattern.test("https://example.com/ax"));
const pattern = new URLPattern({ pathname: "((?<=b)a)" });
console.log(pattern.test("https://example.com/ba"));
console.log(pattern.test("https://example.com/xa"));
const pattern = new URLPattern({ pathname: "((?<!b)a)" });
console.log(pattern.test("https://example.com/ba"));
console.log(pattern.test("https://example.com/xa"));
- Parentheses need to be escaped in range expressions within URLPattern even though they don't in RegExp.
new URLPattern({ pathname: "([()])" });
new URLPattern({ pathname: "([\\(\\)])" });
new RegExp("[()]");
new RegExp("[\\(\\)]");
Unnamed and named groups
Groups can either be named or unnamed. Named groups are specified by prefixing the group name with a colon (:
). Regexp groups that are not prefixed by a colon and a name are unnamed. Unnamed groups are numerically indexed in the match result based on their order in the pattern.
const pattern = new URLPattern("/books/:id(\\d+)", "https://example.com");
console.log(pattern.exec("https://example.com/books/123").pathname.groups);
const pattern = new URLPattern("/books/(\\d+)", "https://example.com");
console.log(pattern.exec("https://example.com/books/123").pathname.groups);
Group modifiers
Groups can also have modifiers. These are specified after the group name (or after the regexp if there is one). There are three modifiers: ?
to make the group optional, +
to make the group repeat one or more times, and *
to make the group repeat zero or more times.
const pattern = new URLPattern("/books/:id?", "https://example.com");
console.log(pattern.test("https://example.com/books/123"));
console.log(pattern.test("https://example.com/books"));
console.log(pattern.test("https://example.com/books/"));
console.log(pattern.test("https://example.com/books/123/456"));
console.log(pattern.test("https://example.com/books/123/456/789"));
const pattern = new URLPattern("/books/:id+", "https://example.com");
console.log(pattern.test("https://example.com/books/123"));
console.log(pattern.test("https://example.com/books"));
console.log(pattern.test("https://example.com/books/"));
console.log(pattern.test("https://example.com/books/123/456"));
console.log(pattern.test("https://example.com/books/123/456/789"));
const pattern = new URLPattern("/books/:id*", "https://example.com");
console.log(pattern.test("https://example.com/books/123"));
console.log(pattern.test("https://example.com/books"));
console.log(pattern.test("https://example.com/books/"));
console.log(pattern.test("https://example.com/books/123/456"));
console.log(pattern.test("https://example.com/books/123/456/789"));
Group delimiters
Patterns can also contain group delimiters. These are pieces of a pattern that are surrounded by curly braces ({}
). These group delimiters are not captured in the match result like capturing groups, but can still have modifiers applied to them, just like groups. If group delimiters are not modified by a modifier, they are treated as if the items in them were just part of the parent pattern. Group delimiters may not contain other group delimiters, but may contain any other pattern items (capturing groups, regex, wildcard, or fixed text).
const pattern = new URLPattern("/book{s}?", "https://example.com");
console.log(pattern.test("https://example.com/books"));
console.log(pattern.test("https://example.com/book"));
console.log(pattern.exec("https://example.com/books").pathname.groups);
const pattern = new URLPattern("/book{s}", "https://example.com");
console.log(pattern.pathname);
console.log(pattern.test("https://example.com/books"));
console.log(pattern.test("https://example.com/book"));
const pattern = new URLPattern({ pathname: "/blog/:id(\\d+){-:title}?" });
console.log(pattern.test("https://example.com/blog/123-my-blog"));
console.log(pattern.test("https://example.com/blog/123"));
console.log(pattern.test("https://example.com/blog/my-blog"));
Automatic group prefixing in pathnames
In patterns that match against the pathname
part of a URL, groups get an automatic slash (/
) prefix added if the group definition is preceded by a slash (/
). This is useful for groups with modifiers, as it allows for repeating groups to work as expected.
If you do not want automatic prefixing, you can disable it by surrounding the group with group delimiters ({}
). Group delimiters do not have automatic prefixing behavior.
const pattern = new URLPattern("/books/:id?", "https://example.com");
console.log(pattern.test("https://example.com/books/123"));
console.log(pattern.test("https://example.com/books"));
console.log(pattern.test("https://example.com/books/"));
const pattern = new URLPattern("/books/:id+", "https://example.com");
console.log(pattern.test("https://example.com/books/123"));
console.log(pattern.test("https://example.com/books/123/456"));
console.log(pattern.test("https://example.com/books/123/"));
console.log(pattern.test("https://example.com/books/123/456/"));
const pattern = new URLPattern({ hash: "/books/:id?" });
console.log(pattern.test("https://example.com#/books/123"));
console.log(pattern.test("https://example.com#/books"));
console.log(pattern.test("https://example.com#/books/"));
const pattern = new URLPattern({ pathname: "/books/{:id}?" });
console.log(pattern.test("https://example.com/books/123"));
console.log(pattern.test("https://example.com/books"));
console.log(pattern.test("https://example.com/books/"));
Wildcard tokens
The wildcard token (*
) is a shorthand for an unnamed capturing group that matches all characters zero or more times. You can place this anywhere in the pattern. The wildcard is greedy, meaning that it will match the longest possible string.
const pattern = new URLPattern("/books/*", "https://example.com");
console.log(pattern.test("https://example.com/books/123"));
console.log(pattern.test("https://example.com/books"));
console.log(pattern.test("https://example.com/books/"));
console.log(pattern.test("https://example.com/books/123/456"));
const pattern = new URLPattern("/*.png", "https://example.com");
console.log(pattern.test("https://example.com/image.png"));
console.log(pattern.test("https://example.com/image.png/123"));
console.log(pattern.test("https://example.com/folder/image.png"));
console.log(pattern.test("https://example.com/.png"));
Pattern normalization
When a pattern is parsed it is automatically normalized to a canonical form. For example, unicode characters are percent encoded in the pathname property, punycode encoding is used in the hostname, default port numbers are elided, paths like /foo/./bar/
are collapsed to just /foo/bar
, etc. In addition, there are some pattern representations that parse to the same underlying meaning, like foo
and {foo}
. Such cases are normalized to the simplest form. In this case {foo}
gets changed to foo
.
Case sensitivity
The URL Pattern API treats many parts of the URL as case-sensitive by default when matching. In contrast, many client-side JavaScript frameworks use case-insensitive URL matching. An ignoreCase
option is available on the URLPattern()
constructor to enable case-insensitive matching if desired.
const pattern = new URLPattern("https://example.com/2022/feb/*");
console.log(pattern.test("https://example.com/2022/feb/xc44rsz"));
console.log(pattern.test("https://example.com/2022/Feb/xc44rsz"));
Setting the ignoreCase
option to true
in the constructor switches all matching operations to case-insensitive for the given pattern:
const pattern = new URLPattern("https://example.com/2022/feb/*", {
ignoreCase: true,
});
console.log(pattern.test("https://example.com/2022/feb/xc44rsz"));
console.log(pattern.test("https://example.com/2022/Feb/xc44rsz"));
Examples
Filter on a specific URL component
The following example shows how a URLPattern
filters a specific URL component. When the URLPattern()
constructor is called with a structured object of component patterns any missing components default to the *
wildcard value.
const pattern = new URLPattern({
hostname: "{*.}?example.com",
});
console.log(pattern.hostname);
console.log(pattern.protocol);
console.log(pattern.username);
console.log(pattern.password);
console.log(pattern.pathname);
console.log(pattern.search);
console.log(pattern.hash);
console.log(pattern.test("https://example.com/foo/bar"));
console.log(pattern.test({ hostname: "cdn.example.com" }));
console.log(pattern.test("custom-protocol://example.com/other/path?q=1"));
console.log(pattern.test("https://cdn-example.com/foo/bar"));
Construct a URLPattern from a full URL string
The following example shows how to construct a URLPattern
from a full URL string with embedded patterns. For example, a :
can be both the URL protocol suffix, like https:
, and the beginning of a named pattern group, like :foo
. It "just works" if there is no ambiguity between whether a character is part of the URL syntax or part of the pattern syntax.
const pattern = new URLPattern("https://cdn-*.example.com/*.jpg");
console.log(pattern.protocol);
console.log(pattern.hostname);
console.log(pattern.pathname);
console.log(pattern.username);
console.log(pattern.password);
console.log(pattern.search);
console.log(pattern.hash);
console.log(
pattern.test("https://cdn-1234.example.com/product/assets/hero.jpg"),
);
console.log(
pattern.test("https://cdn-1234.example.com/product/assets/hero.jpg?q=1"),
);
Constructing a URLPattern with an ambiguous URL string
The following example shows how a URLPattern
constructed from an ambiguous string will favor treating characters as part of the pattern syntax. In this case the :
character could be the protocol component suffix or it could be the prefix for a named group in the pattern. The constructor chooses to treat this as part of the pattern and therefore determines this is a relative pathname pattern. Since there is no base URL the relative pathname cannot be resolved and it throws an error.
const pattern = new URLPattern("data:foo*");
Escaping characters to disambiguate URLPattern constructor strings
The following example shows how an ambiguous constructor string character can be escaped to be treated as a URL separator instead of a pattern character. Here :
is escaped as \\:
.
const pattern = new URLPattern("data\\:foo*");
console.log(pattern.protocol);
console.log(pattern.pathname);
console.log(pattern.username);
console.log(pattern.password);
console.log(pattern.hostname);
console.log(pattern.port);
console.log(pattern.search);
console.log(pattern.hash);
console.log(pattern.test("data:foobar"));
Using base URLs for test() and exec()
The following example shows how test()
and exec()
can use base URLs.
const pattern = new URLPattern({ hostname: "example.com", pathname: "/foo/*" });
console.log(
pattern.test({
pathname: "/foo/bar",
baseURL: "https://example.com/baz",
}),
);
console.log(pattern.test("/foo/bar", "https://example.com/baz"));
try {
pattern.test({ pathname: "/foo/bar" }, "https://example.com/baz");
} catch (e) {}
const result = pattern.exec("/foo/bar", "https://example.com/baz");
console.log(result.pathname.input);
console.log(result.pathname.groups[0]);
console.log(result.hostname.input);
Using base URLs in the URLPattern constructor
The follow example shows how base URLs can also be used to construct the URLPattern
. Note that the base URL in these cases is treated strictly as a URL and cannot contain any pattern syntax itself.
Also, since the base URL provides a value for every component the resulting URLPattern
will also have a value for every component, even if it's the empty string. This means you do not get the "default to wildcard" behavior.
const pattern1 = new URLPattern({
pathname: "/foo/*",
baseURL: "https://example.com",
});
console.log(pattern1.protocol);
console.log(pattern1.hostname);
console.log(pattern1.pathname);
console.log(pattern1.username);
console.log(pattern1.password);
console.log(pattern1.port);
console.log(pattern1.search);
console.log(pattern1.hash);
const pattern2 = new URLPattern("/foo/*", "https://example.com");
try {
const pattern3 = new URLPattern("/foo/*");
} catch (e) {}
Accessing matched group values
The following example shows how input values that match pattern groups can later be accessed from the exec()
result object. Unnamed groups are assigned index numbers sequentially.
const pattern = new URLPattern({ hostname: "*.example.com" });
const result = pattern.exec({ hostname: "cdn.example.com" });
console.log(result.hostname.groups[0]);
console.log(result.hostname.input);
console.log(result.inputs);
Accessing matched group values using custom names
The following example shows how groups can be given custom names which can be used to accessed the matched value in the result object.
const pattern = new URLPattern({ pathname: "/:product/:user/:action" });
const result = pattern.exec({ pathname: "/store/wanderview/view" });
console.log(result.pathname.groups.product);
console.log(result.pathname.groups.user);
console.log(result.pathname.groups.action);
console.log(result.pathname.input);
console.log(result.inputs);
Custom regular expression groups
The following example shows how a matching group can use a custom regular expression.
const pattern = new URLPattern({ pathname: "/(foo|bar)" });
console.log(pattern.test({ pathname: "/foo" }));
console.log(pattern.test({ pathname: "/bar" }));
console.log(pattern.test({ pathname: "/baz" }));
const result = pattern.exec({ pathname: "/foo" });
console.log(result.pathname.groups[0]);
Named group with a custom regular expression
The following example shows how to use a custom regular expression with a named group.
const pattern = new URLPattern({ pathname: "/:type(foo|bar)" });
const result = pattern.exec({ pathname: "/foo" });
console.log(result.pathname.groups.type);
Making matching groups optional
The following example shows how to make a matching group optional by placing a ?
modifier after it. For the pathname component this also causes any preceding /
character to be treated as an optional prefix to the group.
const pattern = new URLPattern({ pathname: "/product/(index.html)?" });
console.log(pattern.test({ pathname: "/product/index.html" }));
console.log(pattern.test({ pathname: "/product" }));
const pattern2 = new URLPattern({ pathname: "/product/:action?" });
console.log(pattern2.test({ pathname: "/product/view" }));
console.log(pattern2.test({ pathname: "/product" }));
const pattern3 = new URLPattern({ pathname: "/product/*?" });
console.log(pattern3.test({ pathname: "/product/wanderview/view" }));
console.log(pattern3.test({ pathname: "/product" }));
console.log(pattern3.test({ pathname: "/product/" }));
Making matching groups repeated
The following example shows how a matching group can be made repeated by placing +
modifier after it. In the pathname
component this also treats the /
prefix as special. It is repeated with the group.
const pattern = new URLPattern({ pathname: "/product/:action+" });
const result = pattern.exec({ pathname: "/product/do/some/thing/cool" });
result.pathname.groups.action;
console.log(pattern.test({ pathname: "/product" }));
Making matching groups optional and repeated
The following example shows how to make a matching group that is both optional and repeated. Do this by placing a *
modifier after the group. Again, the pathname component treats the /
prefix as special. It both becomes optional and is also repeated with the group.
const pattern = new URLPattern({ pathname: "/product/:action*" });
const result = pattern.exec({ pathname: "/product/do/some/thing/cool" });
console.log(result.pathname.groups.action);
console.log(pattern.test({ pathname: "/product" }));
Using a custom prefix or suffix for an optional or repeated modifier
The following example shows how curly braces can be used to denote a custom prefix and/or suffix to be operated on by a subsequent ?
, *
, or +
modifier.
const pattern = new URLPattern({ hostname: "{:subdomain.}*example.com" });
console.log(pattern.test({ hostname: "example.com" }));
console.log(pattern.test({ hostname: "foo.bar.example.com" }));
console.log(pattern.test({ hostname: ".example.com" }));
const result = pattern.exec({ hostname: "foo.bar.example.com" });
console.log(result.hostname.groups.subdomain);
Making text optional or repeated without a matching group
The following example shows how curly braces can be used to denote fixed text values as optional or repeated without using a matching group.
const pattern = new URLPattern({ pathname: "/product{/}?" });
console.log(pattern.test({ pathname: "/product" }));
console.log(pattern.test({ pathname: "/product/" }));
const result = pattern.exec({ pathname: "/product/" });
console.log(result.pathname.groups);
Using multiple components and features at once
The following example shows how many features can be combined across multiple URL components.
const pattern = new URLPattern({
protocol: "http{s}?",
username: ":user?",
password: ":pass?",
hostname: "{:subdomain.}*example.com",
pathname: "/product/:action*",
});
const result = pattern.exec(
"http://foo:[email protected]/product/view?q=12345",
);
console.log(result.username.groups.user);
console.log(result.password.groups.pass);
console.log(result.hostname.groups.subdomain);
console.log(result.pathname.groups.action);
Specifications
Browser compatibility
|
Desktop |
Mobile |
|
Chrome |
Edge |
Firefox |
Internet Explorer |
Opera |
Safari |
WebView Android |
Chrome Android |
Firefox for Android |
Opera Android |
Safari on IOS |
Samsung Internet |
URLPattern |
95 |
95 |
No |
No |
81 |
No |
95 |
95 |
No |
67 |
No |
17.0 |
URL_Pattern_API |
95 |
95 |
No |
No |
81 |
No |
95 |
95 |
No |
67 |
No |
17.0 |
exec |
95 |
95 |
No |
No |
81 |
No |
95 |
95 |
No |
67 |
No |
17.0 |
hash |
95 |
95 |
No |
No |
81 |
No |
95 |
95 |
No |
67 |
No |
17.0 |
hostname |
95 |
95 |
No |
No |
81 |
No |
95 |
95 |
No |
67 |
No |
17.0 |
password |
95 |
95 |
No |
No |
81 |
No |
95 |
95 |
No |
67 |
No |
17.0 |
pathname |
95 |
95 |
No |
No |
81 |
No |
95 |
95 |
No |
67 |
No |
17.0 |
port |
95 |
95 |
No |
No |
81 |
No |
95 |
95 |
No |
67 |
No |
17.0 |
protocol |
95 |
95 |
No |
No |
81 |
No |
95 |
95 |
No |
67 |
No |
17.0 |
search |
95 |
95 |
No |
No |
81 |
No |
95 |
95 |
No |
67 |
No |
17.0 |
test |
95 |
95 |
No |
No |
81 |
No |
95 |
95 |
No |
67 |
No |
17.0 |
username |
95 |
95 |
No |
No |
81 |
No |
95 |
95 |
No |
67 |
No |
17.0 |
See also
- A polyfill of
URLPattern
is available on GitHub - The pattern syntax used by URLPattern is similar to the syntax used by path-to-regexp