Skip to content

Commit c0e071a

Browse files
authored
Support {} for capturing prefix and suffix chars (pillarjs#207)
1 parent 8a3710d commit c0e071a

File tree

4 files changed

+627
-635
lines changed

4 files changed

+627
-635
lines changed

Readme.md

+61-45
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,10 @@ const { pathToRegexp, match, parse, compile } = require("path-to-regexp");
3333
- **strict** When `true` the regexp allows an optional trailing delimiter to match. (default: `false`)
3434
- **end** When `true` the regexp will match to the end of the string. (default: `true`)
3535
- **start** When `true` the regexp will match from the beginning of the string. (default: `true`)
36-
- **delimiter** The default delimiter for segments. (default: `'/'`)
36+
- **delimiter** The default delimiter for segments, e.g. `[^/]` for `:named` patterns. (default: `'/'`)
3737
- **endsWith** Optional character, or list of characters, to treat as "end" characters.
38-
- **whitelist** List of characters to consider delimiters when parsing. (default: `undefined`, any character)
3938
- **encode** A function to encode strings before inserting into `RegExp`. (default: `x => x`)
39+
- **prefixes** List of characters to automatically consider prefixes when parsing. (default: `./`)
4040

4141
```javascript
4242
const keys = [];
@@ -49,11 +49,11 @@ const regexp = pathToRegexp("/foo/:bar", keys);
4949

5050
### Parameters
5151

52-
The path argument is used to define parameters and populate the list of keys.
52+
The path argument is used to define parameters and populate keys.
5353

5454
#### Named Parameters
5555

56-
Named parameters are defined by prefixing a colon to the parameter name (`:foo`). By default, the parameter will match until the next prefix (e.g. `[^/]+`).
56+
Named parameters are defined by prefixing a colon to the parameter name (`:foo`).
5757

5858
```js
5959
const regexp = pathToRegexp("/:foo/:bar");
@@ -65,7 +65,61 @@ regexp.exec("/test/route");
6565

6666
**Please note:** Parameter names must use "word characters" (`[A-Za-z0-9_]`).
6767

68-
#### Parameter Modifiers
68+
##### Custom Matching Parameters
69+
70+
Parameters can have a custom regexp, which overrides the default match (`[^/]+`). For example, you can match digits or names in a path:
71+
72+
```js
73+
const regexpNumbers = pathToRegexp("/icon-:foo(\\d+).png");
74+
// keys = [{ name: 'foo', ... }]
75+
76+
regexpNumbers.exec("/icon-123.png");
77+
//=> ['/icon-123.png', '123']
78+
79+
regexpNumbers.exec("/icon-abc.png");
80+
//=> null
81+
82+
const regexpWord = pathToRegexp("/(user|u)");
83+
// keys = [{ name: 0, ... }]
84+
85+
regexpWord.exec("/u");
86+
//=> ['/u', 'u']
87+
88+
regexpWord.exec("/users");
89+
//=> null
90+
```
91+
92+
**Tip:** Backslashes need to be escaped with another backslash in JavaScript strings.
93+
94+
##### Custom Prefix and Suffix
95+
96+
Parameters can be wrapped in `{}` to create custom prefixes or suffixes for your segment:
97+
98+
```js
99+
const regexp = pathToRegexp("/:attr1?{-:attr2}?{-:attr3}?");
100+
101+
regexp.exec("/test");
102+
// => ['/test', 'test', undefined, undefined]
103+
104+
regexp.exec("/test-test");
105+
// => ['/test', 'test', 'test', undefined]
106+
```
107+
108+
#### Unnamed Parameters
109+
110+
It is possible to write an unnamed parameter that only consists of a regexp. It works the same the named parameter, except it will be numerically indexed:
111+
112+
```js
113+
const regexp = pathToRegexp("/:foo/(.*)");
114+
// keys = [{ name: 'foo', ... }, { name: 0, ... }]
115+
116+
regexp.exec("/test/route");
117+
//=> [ '/test/route', 'test', 'route', index: 0, input: '/test/route', groups: undefined ]
118+
```
119+
120+
#### Modifiers
121+
122+
Modifiers must be placed after the parameter (e.g. `/:foo?`, `/(test)?`, or `/:foo(test)?`).
69123

70124
##### Optional
71125

@@ -86,7 +140,7 @@ regexp.exec("/test/route");
86140

87141
##### Zero or more
88142

89-
Parameters can be suffixed with an asterisk (`*`) to denote a zero or more parameter matches. The prefix is used for each match.
143+
Parameters can be suffixed with an asterisk (`*`) to denote a zero or more parameter matches.
90144

91145
```js
92146
const regexp = pathToRegexp("/:foo*");
@@ -101,7 +155,7 @@ regexp.exec("/bar/baz");
101155

102156
##### One or more
103157

104-
Parameters can be suffixed with a plus sign (`+`) to denote a one or more parameter matches. The prefix is used for each match.
158+
Parameters can be suffixed with a plus sign (`+`) to denote a one or more parameter matches.
105159

106160
```js
107161
const regexp = pathToRegexp("/:foo+");
@@ -114,44 +168,6 @@ regexp.exec("/bar/baz");
114168
//=> [ '/bar/baz','bar/baz', index: 0, input: '/bar/baz', groups: undefined ]
115169
```
116170

117-
#### Unnamed Parameters
118-
119-
It is possible to write an unnamed parameter that only consists of a matching group. It works the same as a named parameter, except it will be numerically indexed.
120-
121-
```js
122-
const regexp = pathToRegexp("/:foo/(.*)");
123-
// keys = [{ name: 'foo', ... }, { name: 0, ... }]
124-
125-
regexp.exec("/test/route");
126-
//=> [ '/test/route', 'test', 'route', index: 0, input: '/test/route', groups: undefined ]
127-
```
128-
129-
#### Custom Matching Parameters
130-
131-
All parameters can have a custom regexp, which overrides the default match (`[^/]+`). For example, you can match digits or names in a path:
132-
133-
```js
134-
const regexpNumbers = pathToRegexp("/icon-:foo(\\d+).png");
135-
// keys = [{ name: 'foo', ... }]
136-
137-
regexpNumbers.exec("/icon-123.png");
138-
//=> ['/icon-123.png', '123']
139-
140-
regexpNumbers.exec("/icon-abc.png");
141-
//=> null
142-
143-
const regexpWord = pathToRegexp("/(user|u)");
144-
// keys = [{ name: 0, ... }]
145-
146-
regexpWord.exec("/u");
147-
//=> ['/u', 'u']
148-
149-
regexpWord.exec("/users");
150-
//=> null
151-
```
152-
153-
**Tip:** Backslashes need to be escaped with another backslash in JavaScript strings.
154-
155171
### Match
156172

157173
The `match` function will return a function for transforming paths into parameters:

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
"size-limit": [
3535
{
3636
"path": "dist/index.js",
37-
"limit": "1.75 kB"
37+
"limit": "2 kB"
3838
}
3939
],
4040
"jest": {

0 commit comments

Comments
 (0)