0

I do not understand the behavior of my JavaScript function.

There is a simple function that checks if a URL matches with a RegExp. It returns a boolean.

    /**
    * Check the url is matching the ProviderURL.
    *
    * @return {boolean}    ProviderURL as RegExp
    */
    this.matchURL = function(url) {
        return !(this.matchException(url)) && urlPattern.test(url);
    };

However, when I call this function, it returns a false in the if-condition, but a true in a console.log.

console.log(providers[i].matchURL(request.url)); //true

if(providers[i].matchURL(request.url)) { 
  console.log("never displayed.");
}

I have now "fixed" this problem with a previous if-condition. Nevertheless, I would like to know why the program behaves this way.

                let match = providers[i].matchURL(request.url);
                //really weird method
                if(match == providers[i].matchURL(request.url));

                if(match)
                {
                    result = removeFieldsFormURL(providers[i], request);
                }

It is a WebExtensions addon.

urlPattern:

    /**
    * Add URL pattern.
    *
    * @require urlPatterns as RegExp
    */
    this.setURLPattern = function(urlPatterns) {
        urlPattern = new RegExp(urlPatterns, "mgi");
    };
  • Show us the `urlPattern` and where it is declared, please – Bergi Jun 16 '18 at 19:47
  • Most likely a duplicate of [Why does a RegExp with global flag give wrong results?](https://stackoverflow.com/q/1520800/1048572) – Bergi Jun 16 '18 at 19:48
  • You're right, it was the global flag of RegExp. I have removed every "g" flag in the entire code and now the program behaves as it should. Thank you :) – RandomHuman123 Jun 16 '18 at 20:06

0 Answers0