/*
* to ilustrate it better, the idea is that the user will pass
* two regex(so, they are dynamic).
* I want to see if there is a way in which we can find out which
* dynamic rx fits better the requirements
*/
let rx = [
/.+\/images\/.+/i,
/.+\/images\/one-specific-image.png/i
];
function match(str){
rx.forEach(function(currentRX){
if(str.match(currentRX)){
return currentRX;
}
});
}
// should return the first rx
match("domain.com/images/something.png");
// should return the second rx, but matches the first rx first
match("domain.com/images/one-specific-image.png");
I was thinking on using the "length" of the rx, but it is not the best option. The most interesting way would be to find out the most complex one. It would be really useful to figure out the "complexity" of each regular expression.
You could order the regexes in order of complexity, filter thru them and pop off the last one - assuming that is the most "complex" ala:
I just re-read your post. The "user" will pass in the regexes, so how do you presume to know if they will follow ordering protocol? So, I guess you could sort the regex on "length" and we can presume that the longest is the most complex? OR we can run thru all the regexes, attach a timestamp, and grab the greatest (assuming greatest would be longest, which would mean "more complex"?).