简体   繁体   English

比较和索引Javascript数组中的正则表达式

[英]Compare and index regex in Javascript array

I have two arrays: 我有两个数组:

enteredCommands = ["valid", "this", 1.1];
validParameters = [/valid/, /alsoValid/, /this|that/, /\d+(\.)?\d*/];

I want to loop through all of the enteredCommands , if it exists in validParameters remove it from validParameters , if it doesn't, break. 我想遍历所有输入的enteredCommands ,如果它存在于validParameters中,则将其从validParameters删除,如果不存在,则中断。

I don't know how to compare the regex in this way, if I change validParameters to: 如果将有效validParameters更改为:我不知道如何以这种方式比较正则表达式

validParameters = ["valid", "alsoValid", /this|that/, /\\d+(\\.)?\\d*/];

and use: 并使用:

var ok2go = true;
// For each entered command...
for (var i = 0; i < commands.length; i++) {
     // Check to see that it is a valid parameter
     if (validParameters.indexOf(commands[i]) === -1) {
         // If not, an invalid command was entered.
         ok2go = false;
         break;
     // If valid, remove from list of valid parameters so as to prevent duplicates.
     } else {
         validParameters.splice(validParameters.indexOf(commands[i]), 1);
     }
     return ok2go;
}

if (ok2go) {
   // do stuff if all the commands are valid
} else {
   alert("Invalid command");
}

It works the way I want it to for the strings, but obviously not for those values that need to be regex. 它以我想要的字符串方式工作,但显然不适用于那些需要使用正则表达式的值。 Is there any way to solve this? 有什么办法解决这个问题?

Test cases: 测试用例:

enteredCommands = ["valid", "this", 1.1, 3];
// Expected Result: ok2go = false because 2 digits were entered

enteredCommands = ["valid", "alsoValid", "x"];
// Expected Result: ok2go = false because x was entered

enteredCommands = ["valid", "alsoValid", 1];
// Expected Result: ok2go = true because no invalid commands were found so we can continue on with the rest of the code

You could filter the given commands and if a regular expression match, the exclude it from the regex array. 您可以过滤给定的命令,如果正则表达式匹配,则将其从正则表达式数组中排除。 Return only commants which does not match with the rest of the regex array. 仅返回与正则表达式数组其余部分不匹配的命令。

 function check(array) { var regex = [/valid/, /alsoValid/, /this|that/, /\\d+(\\.)?\\d*/]; return array.filter(function (a) { var invalid = true; regex = regex.filter(function (r) { if (!r.test(a)) { return true; } invalid = false; }); invalid && alert('invalid command: ' + a); return invalid; }); } console.log(check(["valid", "this", 1.1, 3])); // 2 digits were entered console.log(check(["valid", "alsoValid", "x"])); // x was entered console.log(check(["valid", "alsoValid", 1])); // nothing, no invalid commands were found 

I recommend you split up the checks for a matching regex and the check for a matching string. 我建议您将匹配正则表达式的检查和匹配字符串的检查分开。

Conceptually, you might want to do the following (code not tested) 从概念上讲,您可能想要执行以下操作(未经测试的代码)

var validStringParameters = ["valid", "alsoValid"];
var validRegexMatches = [/valid/, /alsoValid/, /this|that/, /\d+(\.)?\d*/];

var validCommands = enteredcommands.filter(function(command){
if (validStringParameters.indexOf(command) !== -1){
return true;
}
for (var i = 0; i < validRegexMatches.length; i++){
if (command.test(validRegexMatches[i]){
return true;
})
return false;
}
})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM