Let's say I have two arrays: one is the regex and the other one is the input. What, then, is the best way - in terms of performance and readability - to do something like the output?
var regex = [
'/rat/',
'/cat/'
'/dog/',
'/[1-9]/'
]
var texts = [
'the dog is hiding',
'cat',
'human',
'1'
]
the end result is
result = [
'human'
]
Well, what I was thinking was to do something like reduce
:
// loop by text
for (var i = texts.length - 1; i >= 0; i--) {
// loop by regex
texts[i] = regex.reduce(function (previousValue, currentValue) {
var filterbyRegex = new RegExp("\b" + currentValue + "\b", "g");
if (previousValue.toLowerCase().match(filterbyRegex)) {
delete texts[i];
};
return previousValue;
}, texts[i]);
}
But, is that not readable? Maybe there is another way that I haven't thought of.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…