/*
* 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.
If you use a group for the thing that varies, this solution might work:
https://jsbin.com/vewesabipo/edit?js,console