检查多个字符串以匹配多个正则表达式(均为数组)
Check multiple strings for matching multiple regex (both array)
我需要检查数组(字符串)的所有元素是否与任何正则表达式匹配,这些正则表达式也存储在数组中。
这是字符串数组和正则表达式数组(在此示例中,所有三个元素都是相同的正则表达式 - 我知道这没有意义):
let array = [ 'just some', 'strings', 'which should be tested', 'by regex' ];
let regexes = [ /([^.]+)[.\s]*/g, /([^.]+)[.\s]*/g, /([^.]+)[.\s]*/g ];
现在我会像这样做两个 _.each
循环:
_.each(array, function(element) {
_.each(regexes, function(regex) {
let match = regex.exec(element);
if (match && match.length)
doSomething(match);
});
});
但我想要实现的是,如果 只有一个 正则表达式匹配,我想处理这个字符串。所以对于这个毫无意义的正则表达式数组,情况永远不会是这样,因为不会有或三个匹配的正则表达式。
此外,我想知道是否可以避免这种嵌套的每个循环。
更新
示例:
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)
您可以使用 Array#reduce
并计算匹配项。如果count
等于1
,则进一步处理。
var array = ['1. string', 'word', '123'],
regexes = [/([a-z]+)/, /([0-9]+)/];
array.forEach(function (a) {
var match,
count = regexes.reduce(function (count, r) {
var test = r.exec(a);
if (!test) {
return count;
}
match = test;
return count + 1;
}, 0);
count === 1 && console.log(match);
});
您可以组合 Array.prototype.filter
和 Array.prototype.every
:
let array = ['1. string', 'word', '123'],
regexes = [/([a-z]+)/, /([0-9]+)/];
var result = array.filter(str => {
var count = 0;
return regexes.every(reg => {
reg.test(str) && count++;
return count <= 1;
});
});
console.log(result)
我需要检查数组(字符串)的所有元素是否与任何正则表达式匹配,这些正则表达式也存储在数组中。
这是字符串数组和正则表达式数组(在此示例中,所有三个元素都是相同的正则表达式 - 我知道这没有意义):
let array = [ 'just some', 'strings', 'which should be tested', 'by regex' ];
let regexes = [ /([^.]+)[.\s]*/g, /([^.]+)[.\s]*/g, /([^.]+)[.\s]*/g ];
现在我会像这样做两个 _.each
循环:
_.each(array, function(element) {
_.each(regexes, function(regex) {
let match = regex.exec(element);
if (match && match.length)
doSomething(match);
});
});
但我想要实现的是,如果 只有一个 正则表达式匹配,我想处理这个字符串。所以对于这个毫无意义的正则表达式数组,情况永远不会是这样,因为不会有或三个匹配的正则表达式。
此外,我想知道是否可以避免这种嵌套的每个循环。
更新
示例:
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)
您可以使用 Array#reduce
并计算匹配项。如果count
等于1
,则进一步处理。
var array = ['1. string', 'word', '123'],
regexes = [/([a-z]+)/, /([0-9]+)/];
array.forEach(function (a) {
var match,
count = regexes.reduce(function (count, r) {
var test = r.exec(a);
if (!test) {
return count;
}
match = test;
return count + 1;
}, 0);
count === 1 && console.log(match);
});
您可以组合 Array.prototype.filter
和 Array.prototype.every
:
let array = ['1. string', 'word', '123'],
regexes = [/([a-z]+)/, /([0-9]+)/];
var result = array.filter(str => {
var count = 0;
return regexes.every(reg => {
reg.test(str) && count++;
return count <= 1;
});
});
console.log(result)