URLPattern
The URLPattern interface of Azion Runtime: its constructor, its properties, and the methods that match a URL against a pattern.
The URLPattern interface of Azion Runtime matches a URL, or parts of a URL, against a pattern. A pattern can hold capture groups that extract parts of the matched URL, such as the ID in a path. For more information, refer to URLPattern on MDN Web Docs.
Constructor
URLPattern() returns a URLPattern object built from the pattern and the base URL you pass:
| Parameter | Type | Description |
|---|---|---|
input | Object or string | The pattern. An object takes one pattern per URL component, such as { pathname: '/users/:id' }. A string takes the pattern as a URL string, such as /books/:isbn, resolved against baseURL. |
baseURL | String | Base URL for a relative pattern string, such as https://example.com. |
With a base URL, the components the string leaves out come from the base URL: new URLPattern('/books/:isbn', 'https://example.com') has the protocol https, the hostname example.com, and the pathname /books/:isbn.
A pattern takes named groups such as :id, wildcards (*), optional parts in braces such as http{s}?, and a regular expression after a group name, such as :name(\d+).
Properties
Each component property returns the pattern of that component, as you passed it. A component you set to an empty string, such as port, returns an empty string.
| Property | Type | Description |
|---|---|---|
hash | String | Pattern that matches the hash part of a URL. |
hostname | String | Pattern that matches the hostname part of a URL, such as {*.}?example.com. |
password | String | Pattern that matches the password part of a URL. |
pathname | String | Pattern that matches the pathname part of a URL, such as /api/*. |
port | String | Pattern that matches the port part of a URL. |
protocol | String | Pattern that matches the protocol part of a URL, such as http{s}?. |
search | String | Pattern that matches the search part of a URL, such as q=:q. |
username | String | Pattern that matches the username part of a URL. |
hasRegExpGroups | Boolean | true for a pattern that holds a regular expression group, such as /a/(\d+). |
Methods
| Method | Description |
|---|---|
pattern.exec(input) | Returns an object with the matched parts of the URL, or null when the URL does not match. Each component of the result, such as pathname, carries the captured values in groups. |
pattern.test(input) | Returns true when the URL matches the pattern, and false when it does not. |
Both methods take a URL string. exec() also takes an object of URL components: for the pattern { pathname: '/files/:name(\\d+).:ext' }, exec({ pathname: '/files/12.png' }) returns the pathname groups { name: '12', ext: 'png' }.
Example
This handler matches two URLs against a pathname pattern with a named group, then returns the results of test() and exec() with Response.json():
The function returns these values. The id group captures 42, and exec() returns null for the URL that does not match: