Skip to content

Instantly share code, notes, and snippets.

@goliatone
Created October 5, 2014 01:35
Show Gist options
  • Save goliatone/ab1f1bceb7b098c29715 to your computer and use it in GitHub Desktop.
Save goliatone/ab1f1bceb7b098c29715 to your computer and use it in GitHub Desktop.
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