W3cubDocs

/Web APIs

URLPattern

Limited availability

This feature is not Baseline because it does not work in some of the most widely-used browsers.

Note: This feature is available in Web Workers.

The URLPattern interface of the URL Pattern API matches URLs or parts of URLs against a pattern. The pattern can contain capturing groups that extract parts of the matched URL.

More information about the syntax of patterns can be found on the API overview page: URL Pattern API.

Constructor

URLPattern()

Returns a new URLPattern object based on the given pattern and base URL.

Instance properties

hash Read only

A string containing a pattern to match the hash part of a URL.

hasRegExpGroups Read only

A boolean indicating whether or not any of the URLPattern components contain regular expression capturing groups.

hostname Read only

A string containing a pattern to match the hostname part of a URL.

password Read only

A string containing a pattern to match the password part of a URL.

pathname Read only

A string containing a pattern to match the pathname part of a URL.

port Read only

A string containing a pattern to match the port part of a URL.

protocol Read only

A string containing a pattern to match the protocol part of a URL.

A string containing a pattern to match the search part of a URL.

username Read only

A string containing a pattern to match the username part of a URL.

Instance methods

exec()

Returns an object with the matched parts of the URL or null if the URL does not match.

test()

Returns true if the URL matches the given pattern, false otherwise.

Specifications

Browser compatibility

Desktop Mobile
Chrome Edge Firefox Opera Safari Chrome Android Firefox for Android Opera Android Safari on IOS Samsung Internet WebView Android WebView on iOS
URLPattern 95 95 142 81 26 95 142 67 26 17.0 95 26
URLPattern 95 95 142 81 26 95 142 67 26 17.0 95 26
exec 95 95 142 81 26 95 142 67 26 17.0 95 26
hasRegExpGroups 122 122 142 108 26 122 142 81 26 26.0 122 26
hash 95 95 142 81 26 95 142 67 26 17.0 95 26
hostname 95 95 142 81 26 95 142 67 26 17.0 95 26
password 95 95 142 81 26 95 142 67 26 17.0 95 26
pathname 95 95 142 81 26 95 142 67 26 17.0 95 26
port 95 95 142 81 26 95 142 67 26 17.0 95 26
protocol 95 95 142 81 26 95 142 67 26 17.0 95 26
search 95 95 142 81 26 95 142 67 26 17.0 95 26
test 95 95 142 81 26 95 142 67 26 17.0 95 26
username 95 95 142 81 26 95 142 67 26 17.0 95 26

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

© 2005–2025 MDN contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/API/URLPattern