5
5
* do not influence whether or not a URL is matched, but their values are passed through into
6
6
* the matched parameters returned by {@link UrlMatcher#exec exec}.
7
7
*
8
- * Path parameter placeholders can be specified using simple colon syntax or curly brace syntax,
9
- * which optionally allows a regular expression for the parameter to be specified:
8
+ * Path parameter placeholders can be specified using simple colon/catch-all syntax or curly brace
9
+ * syntax, which optionally allows a regular expression for the parameter to be specified:
10
10
*
11
11
* * ':' name - colon placeholder
12
+ * * '*' name - catch-all placeholder
12
13
* * '{' name '}' - curly placeholder
13
14
* * '{' name ':' regexp '}' - curly placeholder with regexp. Should the regexp itself contain
14
15
* curly braces, they must be in matched pairs or escaped with a backslash.
15
16
*
16
17
* Parameter names may contain only word characters (latin letters, digits, and underscore) and
17
- * must be unique within the pattern (across both path and search parameters). In the absence of
18
- * a regular expression, a path parameter matches any number of characters other than '/'.
18
+ * must be unique within the pattern (across both path and search parameters). For colon
19
+ * placeholders or curly placeholders without an explicit regexp, a path parameter matches any
20
+ * number of characters other than '/'. For catch-all placeholders the path parameter matches
21
+ * any number of characters.
19
22
*
20
23
* ### Examples
21
24
*
22
25
* * '/hello/' - Matches only if the path is exactly '/hello/'. There is no special treatment for
23
26
* trailing slashes, and patterns have to match the entire path, not just a prefix.
24
- * * '/user/:id' - Matches '/user/bob' or '/user/1234!!!' or even '/user/' but not '/user'. The
25
- * second path segment will be captured as the parameter 'id'.
27
+ * * '/user/:id' - Matches '/user/bob' or '/user/1234!!!' or even '/user/' but not '/user' or
28
+ * '/user/bob/details'. The second path segment will be captured as the parameter 'id'.
26
29
* * '/user/{id}' - Same as the previous example, but using curly brace syntax.
27
30
* * '/user/{id:[^/]*}' - Same as the previous example.
28
31
* * '/user/{id:[0-9a-fA-F]{1,8}}' - Similar to the previous example, but only matches if the id
29
32
* parameter consists of 1 to 8 hex digits.
30
33
* * '/files/{path:.*}' - Matches any URL starting with '/files/' and captures the rest of the
31
34
* path into the parameter 'path'.
35
+ * * '/files/*path' - ditto.
32
36
*
33
37
* @constructor
34
38
* @param {string } pattern the pattern to compile into a matcher.
39
43
*/
40
44
function UrlMatcher ( pattern ) {
41
45
42
- // Find all placeholders and create a compiled pattern, using either colon or curly syntax:
46
+ // Find all placeholders and create a compiled pattern, using either classic or curly syntax:
47
+ // '*' name
43
48
// ':' name
44
49
// '{' name '}'
45
50
// '{' name ':' regexp '}'
46
51
// The regular expression is somewhat complicated due to the need to allow curly braces
47
52
// inside the regular expression. The placeholder regexp breaks down as follows:
48
- // : (\w+) colon placeholder ($1)
49
- // \{(\w+)(?:\:( ... ))?\} curly brace placeholder ($2 ) with optional regexp ... ($3 )
53
+ // ([:*]) (\w+) classic placeholder ($1 / $2 )
54
+ // \{(\w+)(?:\:( ... ))?\} curly brace placeholder ($3 ) with optional regexp ... ($4 )
50
55
// (?: ... | ... | ... )+ the regexp consists of any number of atoms, an atom being either
51
56
// [^{}\\]+ - anything other than curly braces or backslash
52
57
// \\. - a backslash escape
53
58
// \{(?:[^{}\\]+|\\.)*\} - a matched set of curly braces containing other atoms
54
- var placeholder = / : ( \w + ) | \{ ( \w + ) (?: \: ( (?: [ ^ { } \\ ] + | \\ .| \{ (?: [ ^ { } \\ ] + | \\ .) * \} ) + ) ) ? \} / g,
59
+ var placeholder = / ( [: * ] ) ( \w + ) | \{ ( \w + ) (?: \: ( (?: [ ^ { } \\ ] + | \\ .| \{ (?: [ ^ { } \\ ] + | \\ .) * \} ) + ) ) ? \} / g,
55
60
names = { } , compiled = '^' , last = 0 , m ,
56
61
segments = this . segments = [ ] ,
57
62
params = this . params = [ ] ;
@@ -73,8 +78,8 @@ function UrlMatcher(pattern) {
73
78
// The number of segments is always 1 more than the number of parameters.
74
79
var id , regexp , segment ;
75
80
while ( ( m = placeholder . exec ( pattern ) ) ) {
76
- id = m [ 1 ] || m [ 2 ] ; // IE[78] returns '' for unmatched groups instead of null
77
- regexp = m [ 3 ] || ' [^/]*';
81
+ id = m [ 2 ] || m [ 3 ] ; // IE[78] returns '' for unmatched groups instead of null
82
+ regexp = m [ 4 ] || ( m [ 1 ] == '*' ? '.*' : ' [^/]*') ;
78
83
segment = pattern . substring ( last , m . index ) ;
79
84
if ( segment . indexOf ( '?' ) >= 0 ) break ; // we're into the search part
80
85
compiled += quoteRegExp ( segment ) + '(' + regexp + ')' ;
0 commit comments