You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Some simple introductory examples are added, along with some brief
overview text of both URL patterns and (component-specific) pattern
strings.
Some more detailed examples already exist within the relevant parsing
code.
This contributes to resolving whatwg#127.
<h2 id=urlpattern-class>The {{URLPattern}} class </h2>
100
106
107
+
A {{URLPattern}} consists of several [=components=], each of which represents a [=pattern string|pattern=] which could be matched against the corresponding component of a [=URL=].
108
+
109
+
It can be constructed using a string for each component, or from a shorthand string. It can optionally be resolved relative to a base URL.
110
+
111
+
<div class="example">
112
+
The shorthand "`https://example.com/:category/*`" corresponds to the following components:
typedef (USVString or URLPatternInit) URLPatternInput;
103
145
@@ -833,6 +875,18 @@ To <dfn>compute protocol matches a special scheme flag</dfn> given a [=construct
833
875
834
876
A <dfn>pattern string</dfn> is a string that is written to match a set of target strings. A <dfn for="pattern string">well formed</dfn> pattern string conforms to a particular pattern syntax. This pattern syntax is directly based on the syntax used by the popular [path-to-regexp](https://github.com/pillarjs/path-to-regexp) JavaScript library.
835
877
878
+
It can be [=parse a pattern string|parsed=] to produce a [=part list=] which describes, in order, what must appear in a component string for the pattern string to match.
879
+
880
+
<div class="example">
881
+
Pattern strings can contain capture groups, which by default match the shortest possible string, up to a component-specific separator (`/` in the pathname, `.` in the hostname). For example, the pathname pattern "`/blog/:title`" will match "`/blog/hello-world`" but not "`/blog/2012/02"`.
882
+
883
+
A regular expression can also be used instead, so the pathname pattern "`/blog/:year(\\d+)/:month(\\d+)`" will match "`/blog/2012/02`".
884
+
885
+
A group can also be made optional, or repeated, by using a modifier. For example, the pathname pattern "`/products/:id?"` will match both "`/products`" and "`/products/2`" (but not "`/products/`"). In the pathname specifically, groups automatically require a leading `/`; to avoid this, the group can be explicitly deliminated, as in the pathname pattern "`/products/{:id}?"`.
886
+
887
+
A full wildcard `*` can also be used to match as much as possible, as in the pathname pattern "`/products/*`".
0 commit comments