简体   繁体   English

根据字符串数组检查字符串(密码验证)?

[英]Check string against array of strings (password validation)?

I've been googling around but can't seem to find this answer anywhere, weirdly. 我一直在四处搜寻,但似乎无法在任何地方找到这个答案,很奇怪。 I need to check a single string against an array of strings, to see if any of the array strings are contained by the original string. 我需要针对字符串数组检查单个字符串,以查看原始字符串是否包含任何数组字符串。

So, I need to check if the password contains any of the strings, not if it is equal. 因此,我需要检查密码是否包含任何字符串,而不是是否相等。

Eg: 例如:

var password = 'word2'
var arrayOfStrings = ['word', 'another', 'third']

if (password.indexOf(arrayOfStrings) >= 0) {
  // valid
} else {
  // not valid
}

Wherein password is not valid. 其中password无效。

I assume you try to check if your password contains any word of the array. 我假设您尝试检查密码是否包含数组的任何单词。 Just loop it :) 只是循环它:)

for (var i=0, len=arrayOfStrings; i < len; i++) {
   if (password.indexOf(arrayOfStrings[i]) !=-1) {
      break;
      // EQUALS!!
   } else {
      // NOT EQUALS!!
   }
}

Since ES6, the newest vestion of JavaScript, you can use string.include(string) . 从ES6(JavaScript的最新版本)开始,您可以使用string.include(string) But I have no experience with it. 但是我没有经验。

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

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