URLPattern

The URLPattern interface 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

Note: This feature is available in Web Workers

Constructor

URLPattern()

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

Properties

hash

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

hostname

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

password

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

pathname

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

port

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

protocol

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

search

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

username

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

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 Internet Explorer Opera Safari WebView Android Chrome Android Firefox for Android Opera Android Safari on IOS Samsung Internet
URLPattern
95
93-95
95
93-95
No
No
81
No
No
95
93-95
No
No
No
No
URLPattern
95
93-95
95
93-95
No
No
81
No
No
95
93-95
No
No
No
No
exec
95
93-95
95
93-95
No
No
81
No
No
95
93-95
No
No
No
No
hash
95
93-95
95
93-95
No
No
81
No
No
95
93-95
No
No
No
No
hostname
95
93-95
95
93-95
No
No
81
No
No
95
93-95
No
No
No
No
password
95
93-95
95
93-95
No
No
81
No
No
95
93-95
No
No
No
No
pathname
95
93-95
95
93-95
No
No
81
No
No
95
93-95
No
No
No
No
port
95
93-95
95
93-95
No
No
81
No
No
95
93-95
No
No
No
No
protocol
95
93-95
95
93-95
No
No
81
No
No
95
93-95
No
No
No
No
search
95
93-95
95
93-95
No
No
81
No
No
95
93-95
No
No
No
No
test
95
93-95
95
93-95
No
No
81
No
No
95
93-95
No
No
No
No
username
95
93-95
95
93-95
No
No
81
No
No
95
93-95
No
No
No
No

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–2021 MDN contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/API/URLPattern