Created
October 5, 2014 01:35
-
-
Save goliatone/ab1f1bceb7b098c29715 to your computer and use it in GitHub Desktop.
JavaScript path to regexp: https://github.com/aaronblohowiak/routes.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
window.pathToRegExp = function (path, keys, options) { | |
keys || (keys = []); | |
options || (options = {}); | |
// https://github.com/aaronblohowiak/routes.js | |
path = path | |
.concat('/?') | |
.replace(/\/\(/g, '(?:/') | |
.replace(/(\/)?(\.)?:(\w+)(?:(\(.*?\)))?(\?)?|\*/g, function(_, slash, format, key, capture, optional){ | |
if (_ === "*"){ | |
keys.push(undefined); | |
return _; | |
} | |
keys.push({name:key}); | |
slash = slash || ''; | |
return '' | |
+ (optional ? '' : slash) | |
+ '(?:' | |
+ (optional ? slash : '') | |
+ (format || '') + (capture || '([^/]+?)') + ')' | |
+ (optional || ''); | |
}) | |
.replace(/([\/.])/g, '\\$1') | |
.replace(/\*/g, '(.*)'); | |
var regexp = new RegExp('^' + path + '$', 'i'); | |
regexp.keys = keys; | |
regexp.options = options; | |
return regexp; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment