* @param {string} path - The path to parse. (It is assumed to have query and hash stripped off.) * @param {Object} opts - Options. * @return {Object} - An object containing an array of path parameter names (`keys`) and a regular * expression (`regexp`) that can be used to identify a matching
(path, opts)
| 22 | * Originally inspired by `pathRexp` in `visionmedia/express/lib/utils.js`. |
| 23 | */ |
| 24 | function routeToRegExp(path, opts) { |
| 25 | var keys = []; |
| 26 | |
| 27 | var pattern = path |
| 28 | .replace(/([().])/g, '\\$1') |
| 29 | .replace(/(\/)?:(\w+)(\*\?|[?*])?/g, function(_, slash, key, option) { |
| 30 | var optional = option === '?' || option === '*?'; |
| 31 | var star = option === '*' || option === '*?'; |
| 32 | keys.push({name: key, optional: optional}); |
| 33 | slash = slash || ''; |
| 34 | return ( |
| 35 | (optional ? '(?:' + slash : slash + '(?:') + |
| 36 | (star ? '(.+?)' : '([^/]+)') + |
| 37 | (optional ? '?)?' : ')') |
| 38 | ); |
| 39 | }) |
| 40 | .replace(/([/$*])/g, '\\$1'); |
| 41 | |
| 42 | if (opts.ignoreTrailingSlashes) { |
| 43 | pattern = pattern.replace(/\/+$/, '') + '/*'; |
| 44 | } |
| 45 | |
| 46 | return { |
| 47 | keys: keys, |
| 48 | regexp: new RegExp( |
| 49 | '^' + pattern + '(?:[?#]|$)', |
| 50 | opts.caseInsensitiveMatch ? 'i' : '' |
| 51 | ) |
| 52 | }; |
| 53 | } |
| 54 | |
| 55 | 'use strict'; |
| 56 |