Skip to content

Instantly share code, notes, and snippets.

@James-E-Adams
Created January 26, 2016 14:15
Show Gist options
  • Save James-E-Adams/edd69dd9850e473f9805 to your computer and use it in GitHub Desktop.
Save James-E-Adams/edd69dd9850e473f9805 to your computer and use it in GitHub Desktop.
//regex object which has a regex string for match checking and an array of params that looks like:
//params = [null,param1,param2,..,param(n)[
//method for creating a reg ex object (as described in the use case constructor), from a given resource path.
function create_reg(resource) {
var folders = resource.split('/');
var reg_string = '^';
var reg_obj={};
var params=[null];
for (var i = 0; i < folders.length; i++) {
//standard
if (folders[i][0] != ':') reg_string += folders[i];
//param
if (folders[i][0] === ':') {
reg_string += '(\\w*)';
//take the param name not including the : and add it to the params list.
params.push(folders[i].substr(1));
}
//add on an escaped slash if we're not on the last directory in the route.
if (i != folders.length - 1) reg_string += '\/';
}
reg_string += '';
reg_obj.reg=new RegExp(reg_string);
reg_obj.params=params;
return reg_obj;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment