Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/435.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 检查多个字符串以匹配多个正则表达式(两个数组)_Javascript_Arrays_Regex_Match_Each - Fatal编程技术网

Javascript 检查多个字符串以匹配多个正则表达式(两个数组)

Javascript 检查多个字符串以匹配多个正则表达式(两个数组),javascript,arrays,regex,match,each,Javascript,Arrays,Regex,Match,Each,我需要检查数组的所有元素(字符串)是否与任何正则表达式匹配,正则表达式也存储在数组中 下面是字符串数组和正则表达式数组(在本例中,所有三个元素都是相同的正则表达式-我知道这没有意义): 现在,我将执行两个\循环,每个循环如下: _.each(array, function(element) { _.each(regexes, function(regex) { let match = regex.exec(element); if (match &

我需要检查数组的所有元素(字符串)是否与任何正则表达式匹配,正则表达式也存储在数组中

下面是字符串数组和正则表达式数组(在本例中,所有三个元素都是相同的正则表达式-我知道这没有意义):

现在,我将执行两个
\循环,每个循环如下:

_.each(array, function(element) {
    _.each(regexes, function(regex) {
        let match = regex.exec(element);
        if (match && match.length)
            doSomething(match);
    });
});
但我想要实现的是,如果只有一个regex匹配,我想处理这个字符串。因此,对于这个无意义的正则表达式数组,情况永远不会是这样,因为没有或三个匹配的正则表达式

此外,如果可能的话,我想知道knwo是否可以避免这种嵌套的each循环

更新

例如:

let array = [ '1. string', 'word', '123' ]
let regexes = [/([a-z]+)/, /([0-9]+)/]

array[0] should NOT pass the test, as both regex are matching
array[1] should pass the test, as just ONE regex is matching
array[2] should pass the test, as just ONE regex is matching
因此,只有数组[1]和数组[2]的结果应该用于进一步处理
doSomething(match)

您可以使用并计算匹配项。如果
count
等于
1
,则进一步处理

var数组=['1.string','word','123'],
正则表达式=[/([a-z]+)/,/([0-9]+)/];
array.forEach(函数(a){
变量匹配,
count=regexes.reduce(函数(count,r){
var测试=r.exec(a);
如果(!测试){
返回计数;
}
匹配=测试;
返回计数+1;
}, 0);
计数===1&&console.log(匹配);

});
您可以组合
Array.prototype.filter
Array.prototype.every

let array=['1.string','word','123'],
正则表达式=[/([a-z]+)/,/([0-9]+)/];
var result=array.filter(str=>{
var计数=0;
返回regex.every(reg=>{
注册测试(str)和计数++;

返回计数我是否需要
var=match
?我不能只使用
regexes.some(r=>r.exec(a))吗
?仅当您喜欢使用
匹配
时,一些
检查数组中的任何元素是否通过测试,对吗?如果一个正则表达式与字符串匹配或多个正则表达式与字符串匹配,则结果将是相同的,对吗?但我想检查是否只有一个正则表达式与字符串匹配。如果多个正则表达式匹配,则应该检查也是无效的,因为没有匹配。我不明白,应该使用所有正则表达式还是只使用一个正则表达式?如果只有一个正则表达式匹配,我想处理这个字符串,看起来只有一个必须匹配。对不起,我的解释不好。我想检查所有字符串-到目前为止没有问题。每个正则表达式都应该检查每个字符串。通常只有be一个匹配的正则表达式。但是如果输入数据中有错误,一切都可能会混乱。这就是为什么我想确保只有一个匹配的正则表达式。在我的示例中,由于有三个相同的正则表达式,结果应该是无效的,因为所有三个正则表达式都匹配。应该只有一个匹配的正则表达式用于进一步处理。如果没有匹配或多个匹配,则不会处理任何内容。
let array = [ '1. string', 'word', '123' ]
let regexes = [/([a-z]+)/, /([0-9]+)/]

array[0] should NOT pass the test, as both regex are matching
array[1] should pass the test, as just ONE regex is matching
array[2] should pass the test, as just ONE regex is matching