Skip to content

Instantly share code, notes, and snippets.

@felipenmoura
Last active September 3, 2016 21:50
Show Gist options
  • 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.

@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