Match patterns

Match patterns are a way to specify groups of URLs: a match pattern matches a specific set of URLs. They are used in WebExtensions APIs in a few places, most notably to specify which documents to load content scripts into, and to specify which URLs to add webRequest listeners to.

APIs that use match patterns usually accept a list of match patterns, and perform the appropriate action if the URL matches any of the patterns. See, for example, the content_scripts key in manifest.json.

Where match patterns are used

Match patterns are used in several places across the manifest.json keys and JavaScript APIs:

Note: The events.UrlFilter object, used for example to filter webNavigation events, looks similar. However, it uses a different mechanism. It matches against URL components using properties such as hostContains and urlMatches (a regular expression), rather than match pattern syntax.

Match pattern structure

Note: Some browsers don't support certain schemes. Check the Browser compatibility table for details.

All match patterns are specified as strings. Apart from the special <all_urls> pattern, match patterns consist of three parts: scheme, host, and path. The scheme and host are separated by ://.

<scheme>://<host><path>

URLs that don't start with <scheme>:// are matched by patterns of the form <scheme>:<path>. For example, data:* matches all data URLs, but data://* does not. Such patterns are mainly useful in APIs that accept any scheme.

scheme

The scheme component may take one of two forms:

Form Matches
* All browsers match "http" and "https".
Firefox also matches "ws" and "wss".
One of http, https, ws, wss, ftp, or file. Only the given scheme, where:
  • Firefox supports "http", "https", "ws", "wss", "file", and "ftp".
  • Chrome supports "http", "https", "ws", "wss", "file", and "ftp".
  • Safari supports "http" and "https".

APIs that accept any scheme

Some APIs accept match patterns with any validly formatted scheme, not only the schemes listed in the table:

For example, you can use these APIs to find tabs with about: or blob: URLs, or to show a context menu item for magnet: links.

Content scripts can't match about:, data:, or blob: documents by their URL. To run a content script in these documents, use match_origin_as_fallback (or matchOriginAsFallback in the scripting API), which matches the document's origin instead.

host

The host component may take one of these forms:

Form Matches
* Any host.
*. followed by part of the hostname, optionally including a port. The given host (and port) and any of its subdomains.
A complete hostname, without wildcards, optionally including a port. Only the host (and port).

Note: Firefox doesn't support the inclusion of a port number due to (Firefox bug 1362809) and (Firefox bug 1468162).

host is optional only if the scheme is "file".

Note that the wildcard may only appear at the start.

path

The path component must begin with a /.

After that, it may subsequently contain any combination of the * wildcard and any of the characters that are allowed in URL paths or query strings. Unlike host, the path component may contain the * wildcard in the middle or at the end, and the * wildcard may appear more than once.

The value for path matches the string that is the URL path plus the URL query string. This includes the ? between the two, if the query string is present in the URL. For example, if you want to match URLs on any domain where the URL path ends with foo.bar, then you need to use an array of Match Patterns like ["*://*/*foo.bar", "*://*/*foo.bar?*"]. The ?* is needed, rather than just bar*, to anchor the ending * to the URL query string and not some portion of the URL path.

Neither the URL fragment identifier nor the # that precedes it are considered part of the path and are ignored during pattern matching. A match pattern containing # fails to match any URL.

<all_urls>

The special value <all_urls> matches all URLs with these schemes:

  • Firefox and Chrome: matches "http", "https", "ws", "wss", "ftp", and "file". In APIs that accept any scheme, it matches URLs with any scheme.
  • Safari only matches "http" and "https".

Note: Access to file:// URLs requires a user permission grant beyond declaring <all_urls> or a file:// match pattern:

  • In Chrome, the user must enable "Allow access to file URLs" for the extension on the chrome://extensions page.
  • In Firefox 153 and later, the user must enable "Access local files on your computer". See extension.isAllowedFileSchemeAccess for more information.

Examples

Pattern Example matches Example non-matches

<all_urls>

Match all URLs.

http://example.org/

https://a.org/some/path/

ws://sockets.somewhere.org/

wss://ws.example.com/stuff/

ftp://files.somewhere.org/

resource://a/b/c/
(unsupported scheme)

ftps://files.somewhere.org/
(unsupported scheme)

*://*/*

Match all HTTP, HTTPS, and WebSocket URLs.

http://example.org/

https://a.org/some/path/

ws://sockets.somewhere.org/

wss://ws.example.com/stuff/

ftp://ftp.example.org/
(unmatched scheme)

file:///a/
(unmatched scheme)

*://*.mozilla.org/*

Match all HTTP, HTTPS, and WebSocket URLs that are hosted at "mozilla.org" or one of its subdomains.

http://mozilla.org/

https://mozilla.org/

http://a.mozilla.org/

http://a.b.mozilla.org/

https://b.mozilla.org/path/

ws://ws.mozilla.org/

wss://secure.mozilla.org/something

ftp://mozilla.org/
(unmatched scheme)

http://mozilla.com/
(unmatched host)

http://firefox.org/
(unmatched host)

*://mozilla.org/

Match all HTTP, HTTPS, and WebSocket URLs that are hosted at exactly "mozilla.org/".

http://mozilla.org/

https://mozilla.org/

ws://mozilla.org/

wss://mozilla.org/

ftp://mozilla.org/
(unmatched scheme)

http://a.mozilla.org/
(unmatched host)

http://mozilla.org/a
(unmatched path)

https://mozilla.org:8080/

Match all HTTPS URLs that are hosted at "mozilla.org/" on port 8080. Note: Ports are supported in Chrome, not in Firefox.

https://mozilla.org:8080/

http://a.mozilla.org/
(unmatched host)

http://mozilla.org:8081
(unmatched host)

ftp://mozilla.org/

Match only "ftp://mozilla.org/".

ftp://mozilla.org

http://mozilla.org/
(unmatched scheme)

ftp://sub.mozilla.org/
(unmatched host)

ftp://mozilla.org/path
(unmatched path)

https://*/path

Match HTTPS URLs on any host whose path is "path".

https://mozilla.org/path

https://a.mozilla.org/path

https://something.com/path

http://mozilla.org/path
(unmatched scheme)

https://mozilla.org/path/
(unmatched path)

https://mozilla.org/a
(unmatched path)

https://mozilla.org/
(unmatched path)

https://mozilla.org/path?foo=1
(unmatched path due to URL query string)

https://*/path/

Match HTTPS URLs on any host whose path is "path/" and which has no URL query string.

https://mozilla.org/path/

https://a.mozilla.org/path/

https://something.com/path/

http://mozilla.org/path/
(unmatched scheme)

https://mozilla.org/path
(unmatched path)

https://mozilla.org/a
(unmatched path)

https://mozilla.org/
(unmatched path)

https://mozilla.org/path/?foo=1
(unmatched path due to URL query string)

https://mozilla.org/*

Match HTTPS URLs only at "mozilla.org", with any URL path and URL query string.

https://mozilla.org/

https://mozilla.org/path

https://mozilla.org/another

https://mozilla.org/path/to/doc

https://mozilla.org/path/to/doc?foo=1

http://mozilla.org/path
(unmatched scheme)

https://mozilla.com/path
(unmatched host)

https://mozilla.org/a/b/c/

Match only this URL, or this URL with any URL fragment.

https://mozilla.org/a/b/c/

https://mozilla.org/a/b/c/#section1

Anything else.

https://mozilla.org/*/b/*/

Match HTTPS URLs hosted on "mozilla.org", whose path contains a component "b" somewhere in the middle. Matches URLs with query strings, if the string ends in a /.

https://mozilla.org/a/b/c/

https://mozilla.org/d/b/f/

https://mozilla.org/a/b/c/d/

https://mozilla.org/a/b/c/d/#section1

https://mozilla.org/a/b/c/d/?foo=/

https://mozilla.org/a?foo=21314&bar=/b/&extra=c/

https://mozilla.org/b/*/
(unmatched path)

https://mozilla.org/a/b/
(unmatched path)

https://mozilla.org/a/b/c/d/?foo=bar
(unmatched path due to URL query string)

file:///blah/*

Match any FILE URL whose path begins with "blah".

file:///blah/

file:///blah/bleh

file:///bleh/
(unmatched path)

Invalid or unmatched patterns

Pattern Issue Reason
resource://path/ Invalid Unsupported scheme.
https://mozilla.org Invalid No path.
https://www.mozilla.org/#section1 Unmatched Contains a reference fragment: the URL that the pattern is matched against has any reference fragment removed before matching.
https://mozilla.*.org/ Invalid "*" in host must be at the start.
https://*zilla.org/ Invalid "*" in host must be the only character or be followed by ".".
http*://mozilla.org/ Invalid "*" in scheme must be the only character.
*://* Invalid Empty path: this should be *://*/*.
file://* Invalid Empty path: this should be file:///*. file://* is accepted when declared in host_permissions in Chrome, which auto-corrects the entry to file:///*.

Browser compatibility