简体   繁体   English

如何在字符串中搜索字符串数组中包含的所有值? (JavaScript)的

[英]How to search a string for all values contained in an Array of strings? (Javascript)

I am trying to learn if there is a javascript method to search a string to see if that string contains any of the values in an array of strings. 我试图了解是否有一个javascript方法来搜索字符串,以查看该字符串是否包含字符串数组中的任何值。

An example would be: 一个例子是:

var a = 'How now brown cow';
var b = new Array('red', 'green', 'brown');

The resulting function would return true because the word brown is contained within the string a . 由于字符串a包含单词brown,因此结果函数将返回true。

More specifically what I am trying to do (except using values from form input) is: 更具体地说,我尝试做的事情(除了使用表单输入中的值)是:

var a = '12345@gmail.com';
var b = new Array('.com', '.net', '.org');

This should also return true. 这也应该返回true。 Then based on this I will go on to accept var a as valid. 然后基于此,我将继续接受var a为有效。

My actual code as of right now (which always returns null ) is as follows: 截至目前我的实际代码(始终返回null )如下:

function check_domain(){
  for(var i=0; i<domains.length; i++){
    var d = domains[i];
    if(elemen.value.indexOf(d) != d){
      return null;
    }
    else{
      vEmail.style.visibility = 'visible';
    }
  }
}

  window.domains = new Array(....Array values here....);

You can create a regular expression from the array: 您可以从数组创建正则表达式:

var re = new RegExp(domains.join('|').replace(/\./g,'\\.'));

Then you can test a string: 然后您可以测试一个字符串:

var a = '12345@gmail.com';
var found = re.test(a);

Not sure why you've got .indexOf(d) != d . 不知道为什么你有.indexOf(d) != d Shouldn't it be: 不应该是:

function check_domain(){
   var d, i;
   for(i = 0; i < domains.length; i++){
      d = domains[i];
      if(elemen.value.indexOf(d) != -1) {
        return true;
      }
   }
}

Yes, there is something called Regular Expressions, if you really need it to match values in an array you can create a loop and check for each word against the string, something like this: 是的,有一些叫做正则表达式的东西,如果你真的需要它来匹配数组中的值,你可以创建一个循环并检查字符串中的每个单词,如下所示:

var a = 'How now brown cow';
var b = new Array('red', 'green', 'brown');

function Check(target, lookFor)
{
    for(var I = 0, L = lookFor.length; I <= L; I++)
        if(new RegExp(lookFor[I]).test(target))
            return true;
    return false;
}

alert(Check(a, b));

But in your case, the best way to go about this is just to join all the words you want to look for in the target string inside only one regular expression, so you avoid using a loop. 但是对于您而言,最好的方法是仅在一个正则表达式内将目标字符串中要查找的所有单词连接起来,因此避免使用循环。

if(/(\.com|\.net|\.org)$/.test("sfdsdfsdf@gmail.com"))
{
// Valid email
}
else
{
// Invalid email
}

暂无
暂无

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

相关问题 搜索字符串以查找数组中包含的字符串 - Search string for strings contained in an array 如何搜索Javascript数组,并返回以X开头的数组中的所有字符串 - How to search Javascript Array, and return all strings in array that start with X 如何在一个字符串中搜索第二个字符串中包含的所有单词? - How to search one string for all of the words contained in a second string? 如果数组中的所有值都为真(字符串)并且其中一个值为假(字符串),则如何返回 boolean true 停止使用 Javascript 检查 - How to return a boolean true if all the values in an array are true (strings) and if one of the value is false(string) stop checking using Javascript 如何使用JavaScript将数字划分为包含在数组中的特定值 - How to divide a number into specific values contained in an array with javascript 在 JavaScript 中的所有键值中搜索对象数组 - Search in array of objects in JavaScript in all keys values 如何在字符串数组中搜索字符串 - How to search for a string inside an array of strings 如何使用字符串数组(js)搜索字符串? - How to search through a string with an array of strings (js)? Javascript从值数组中搜索字符串 - Javascript search in string from array of values 搜索字符串键数组以匹配变量,并使用javascript / jquery返回所有值 - Search array of string keys for matching variable and return all values using javascript / jquery
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM