I'm trying to check efficiently if a string matches any of an array of regexes and return true in the first encountered match (Breaking the iteration over the regexes)
My current code:
_.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 is an array of regexes.
Any suggestions?
Thanks in advance
I'm trying to check efficiently if a string matches any of an array of regexes and return true in the first encountered match (Breaking the iteration over the regexes)
My current code:
_.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 is an array of regexes.
Any suggestions?
Thanks in advance
Share Improve this question edited Nov 10, 2015 at 18:00 Itay Weiss asked Nov 10, 2015 at 17:50 Itay WeissItay Weiss 6881 gold badge9 silver badges18 bronze badges 4-
so, you want to have
var regexes = [/a/,/b/,/c/]
andvar string = "teststring"
and see which regexes match true? – Kristian Commented Nov 10, 2015 at 18:12 -
Wait, is
client.interests
orinterest
an array of regexes? – Felix Kling Commented Nov 10, 2015 at 18:18 - @Kristian I don't really care which one matches I just wanna know if any of them matches. FelixKling The client.interests is the array of regexes. – Itay Weiss Commented Nov 10, 2015 at 18:20
- Cool, then I'll stick with my answer submission. – Kristian Commented Nov 10, 2015 at 18:32
3 Answers
Reset to default 4You could use _.some
, when the function passed returns a truthy value iteration will stop and true will be returned. If it can't find a truthy value it will return false, after iterating through the entire array.
Example:
_.forEach(self._connectedClients, function(client) {
if (client.authenticated) {
if (_.some(client.interests, _.method('test', evt.event_type))) {
self._sendJSON(client.socket, data);
}
}
});
Just use Array#some
:
some()
executes the callback function once for each element present in the array until it finds one where callback returns a true value. If such an element is found,some()
immediately returnstrue
.
var interested = client.interests.some(function(regex) {
return regex.test(evt.event_type);
});
Of course you can also use lodash's _.some
implementation.
If I'm understanding you correctly, using plain javascript, you could do the following to check for matches of a single string over multiple regular expressions.
for(var i = 0; i<x.length; i++) {
var regex = x[i]; console.log('regex', regex);
if( str.match( x[i] ) ) {
console.log("regex:",x[i]," matching: true");
} else {
console.log("regex:",x[i]," matching: false");
}
}
However, if you meant something else, then do clarify. :)