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 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) – 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
2 Answers
Reset to default 7Try 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
版权声明:本文标题:Javascript if arrays contains string with regex - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741289552a2370468.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论