I would like to know how to match a string against an array of regular expressions.
I know how to do this looping through the array.
I also know how to do this by ma
(Many years later)
My version of @Gaby's answer, as I needed a way to check CORS origin against regular expressions in an array:
var corsWhitelist = [/^(?:.+\.)?domain\.com/, /^(?:.+\.)?otherdomain\.com/];
var corsCheck = function(origin, callback) {
if (corsWhitelist.some(function(item) {
return (new RegExp(item).test(origin));
})) {
callback(null, true);
}
else {
callback(null, false);
}
}
corsCheck('otherdomain.com', function(err, result) {
console.log('CORS match for otherdomain.com: ' + result);
});
corsCheck('forbiddendomain.com', function(err, result) {
console.log('CORS match for forbiddendomain.com: ' + result);
});