【发布时间】:2016-02-11 16:59:26
【问题描述】:
我正在尝试有效地检查一个字符串是否匹配任何一个正则表达式数组并在第一次遇到的匹配中返回 true(打破正则表达式的迭代)
我当前的代码:
_.forEach(self._connectedClients, function(client) {
if (client.authenticated) {
var interested = _.forEach(client.interests, function(interest) {
if (evt.event_type.search(interest) != -1) {
return true;
}
});
if (interested) {
self._sendJSON(client.socket, data);
}
}
});
Interest 是一个正则表达式数组。
有什么建议吗?
提前致谢
【问题讨论】:
-
那么,您想拥有
var regexes = [/a/,/b/,/c/]和var string = "teststring"并查看哪些正则表达式匹配正确? -
等等,
client.interests或interest是一个正则表达式数组吗? -
@Kristian 我真的不在乎哪一个匹配我只想知道它们是否匹配。 FelixKling client.interests 是正则表达式的数组。
-
酷,那么我会坚持我的答案提交。
标签: javascript arrays regex node.js lodash