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');
});
}
}
}
callback(). Just posting my code waitvar arr = [/test/, /foo/, /bar/, /news-\d+/];and then do anarr.forEach(re => re.test(...))orfor (re of arr) { re.test(...) }(with the benefit that the latter will let you break out of the loop, while the former won't)