admin管理员组

文章数量:1287595

I want to found if the variable url matches to any value of the array
For example, here's my array:

var arr = ["test", "foo", "bar", "news-[0-999999999999999]*"];

... and my string

var url = "news-294";

I need to check for each arr value if it matches to url, with regex

So I've tried a foreach on the array, new RegExp for each value and then check if it matchs. But It didn't worked well, and It was leaking my browser memory (the CPU was at 24% for a blank page with the script)

How can I do that? Thanks

EDIT: here's my code:

var CMS = {
    RegexMatch: function(regex, str, callback) {
        while ((m = regex.exec(str)) !== null) {
            if(m[0] == str) callback();
        }
    },
    ArrayFindRegex: function(array, str, callback) {
        array.forEach(function(e) {
            CMS.RegexMatch(new RegExp(e), str, function() {
                callback();
            });
        });
    },
        Check: function() {
                var url = "blabla";
                CMS.ArrayFindRegex(["test", "foo", "bar", "news-[0-999999999999999]*"], url, function() {
                    console.log('wow');
                });
            }
        }
}

I want to found if the variable url matches to any value of the array
For example, here's my array:

var arr = ["test", "foo", "bar", "news-[0-999999999999999]*"];

... and my string

var url = "news-294";

I need to check for each arr value if it matches to url, with regex

So I've tried a foreach on the array, new RegExp for each value and then check if it matchs. But It didn't worked well, and It was leaking my browser memory (the CPU was at 24% for a blank page with the script)

How can I do that? Thanks

EDIT: here's my code:

var CMS = {
    RegexMatch: function(regex, str, callback) {
        while ((m = regex.exec(str)) !== null) {
            if(m[0] == str) callback();
        }
    },
    ArrayFindRegex: function(array, str, callback) {
        array.forEach(function(e) {
            CMS.RegexMatch(new RegExp(e), str, function() {
                callback();
            });
        });
    },
        Check: function() {
                var url = "blabla";
                CMS.ArrayFindRegex(["test", "foo", "bar", "news-[0-999999999999999]*"], url, function() {
                    console.log('wow');
                });
            }
        }
}
Share Improve this question edited Apr 13, 2016 at 19:46 asked Apr 13, 2016 at 19:30 user5672329user5672329 6
  • 1 Add your code to the question so we can see what happened. Also, what do you want to happen if it finds a match? – Andy Commented Apr 13, 2016 at 19:31
  • execute callback(). Just posting my code wait – user5672329 Commented Apr 13, 2016 at 19:32
  • 2 If you want to do regexp testing, why start with strings? Just store regexp: var arr = [/test/, /foo/, /bar/, /news-\d+/]; and then do an arr.forEach(re => re.test(...)) or for (re of arr) { re.test(...) } (with the benefit that the latter will let you break out of the loop, while the former won't) – Mike 'Pomax' Kamermans Commented Apr 13, 2016 at 19:37
  • @Mike'Pomax'Kamermans Can you make an answer please? – user5672329 Commented Apr 13, 2016 at 19:40
  • @MrZ, Check your curly braces. It seems you have some error in defining CMS as well. – Mojtaba Commented Apr 13, 2016 at 19:44
 |  Show 1 more ment

2 Answers 2

Reset to default 7

Try Array.prototype.filter instead. It's more idomatic than each (or other iteration methods, in this case). filter returns an array with the contents being any matches or empty if none are found.

var arr = ["test", "foo", "bar", "news-[0-999999999999999]*"];
var url = "news-294";

var matches = arr.filter(function(pattern) {
  return new RegExp(pattern).test(url);
})

console.log(matches);

The above example logs ["news-[0-999999999999999]*"]. You can assert a positive result based off the length of matches.

If you intend to do regular expression matching, start with regular expressions, not strings.

var arr = [/test/, /foo/, /bar/, /news-\d+/];

Then you can do normal matching without weird string rewrites or intepretations:

var matched = arr.some(re => re.test(...))
if (matched) {
  doSomeThing()
}

or

for (re of arr) {
  if (re.test(...)) {
    doSomeThing();
    break;
  }
}

本文标签: Javascript if arrays contains string with regexStack Overflow