简体   繁体   English

如何执行所有可能的组合?

[英]How to execute for all possible combinations?

I've got this code below that generates a random 5-character code.我在下面有这个代码,它生成一个随机的 5 个字符的代码。 For example AU330 , UEEHB , 2EH8D HJ1LM .例如AU330UEEHB2EH8D HJ1LM

function makeid(length) {
   var result           = ''
   var characters       = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'
   var charactersLength = characters.length;
   for ( var i = 0; i < length; i++ ) {
      result += characters.charAt(Math.floor(Math.random() * charactersLength))
   }
   return result
}

alert(makeid(5)); 

What I want to do is check each possible code for U330 and for each code that contains it, log it to console.我想要做的是检查U330每个可能代码以及包含它的每个代码,将其记录到控制台。

Expected Output:预期输出:

AU330 AU330

BU330 BU330

CU330 CU330

DU330 DU330

... ...

You could simply search for a substring with indexOf() function or check for the exact string with regular expressions.您可以简单地使用 indexOf() 函数搜索子字符串或使用正则表达式检查确切的字符串。

var input = "AU330";

console.log(input.indexOf("U330") !== -1); //Indexof
console.log(/[A-Z0-9]U330/.test(input)); //Regex

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

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