在
JavaScript中有一种方法来获取布尔值来匹配字符串与正则表达式数组的匹配吗?
这个例子将是(其中’if’语句表示我正在尝试实现的)
var thisExpressions = [ '/something/','/something_else/','/and_something_else/']; var thisString = 'else'; if (matchInArray(thisString,thisExpressions)) { }
解决方法
http://jsfiddle.net/9nyhh/1/
var thisExpressions = [/something/,/something_else/,/and_something_else/]; var thisExpressions2 = [/else/,/and_something_else/]; var thisString = 'else'; function matchInArray(string,expressions) { var len = expressions.length,i = 0; for (; i < len; i++) { if (string.match(expressions[i])) { return true; } } return false; }; setTimeout(function() { console.log(matchInArray(thisString,thisExpressions)); console.log(matchInArray(thisString,thisExpressions2)); },200)