/*
* 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.
Using the processing time or the length, as proposed by @dsacramone would work, but I am not sure they would be 100% reliable!
I was thinking on comparing the number of groups, indeed, and I believe that will do it!
I believe the solution from @BrenoC makes total sense! I will create a bunch of unit tests so we can assure it will be fully reliable!
:)