Skip to content

Instantly share code, notes, and snippets.

@felipenmoura
Last active September 3, 2016 21:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save felipenmoura/e02c8d8cfdea101fc6265a94321e02df to your computer and use it in GitHub Desktop.
Save felipenmoura/e02c8d8cfdea101fc6265a94321e02df to your computer and use it in GitHub Desktop.
/*
 * 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.

@dsacramone
Copy link

dsacramone commented Jul 2, 2016

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:

 let rx = [
      /.+\/images\/.+/i,
      /.+\/images\/one-specific-image.png/i
    ];

function match(str){
    return rx.filter((currentRX) => {
        if(str.match(currentRX)){
            return currentRX;
        }
    }).pop();
}

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"?).

@klzns
Copy link

klzns commented Jul 2, 2016

If you use a group for the thing that varies, this solution might work:
https://jsbin.com/vewesabipo/edit?js,console

@felipenmoura
Copy link
Author

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!
:)

@klzns
Copy link

klzns commented Jul 3, 2016

I'm interested in the results! Please, share if you can!

@felipenmoura
Copy link
Author

@BrenoC I added it to the DSW project, here https://github.com/NascHQ/dsw/
The project is focused on helping developers to create and use service workers and develop their offline web apps.
The module is this one: https://github.com/NascHQ/dsw/blob/development/src/best-matching-rx.js

Feel free to contribute :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment