代码之家  ›  专栏  ›  技术社区  ›  Shiv Singh

通过jquery或js按逗号分隔的值匹配或搜索字符串

  •  1
  • Shiv Singh  · 技术社区  · 6 年前

    我想通过js数组部分匹配/搜索字符串。下面是我的字符串和数组示例

    var str = "host, gmail, yahoo";
    var search = 'new@gmail.com';
    

    我已经试过如下方法:

    if( str.split(',').indexOf(search) > -1 ) {
       console.log('Found');
    }
    

    它应该与 gmail 对于字符串 new@gmail.com

    https://stackoverflow.com/a/13313857/2384642

    5 回复  |  直到 6 年前
        1
  •  2
  •   Rory McCrossan Hsm Sharique Hasan    6 年前

    这里有一些问题。首先,您的输入字符串在逗号后有空格,但是您正在按 只是 所以你会得到逗号 ' gmail' 作为一个值,这将打破 indexOf() 结果。要么删除空格,要么使用 split(', ')

    第二,您需要从 split() 操作并检查 search 索引() new@gmail.com 在内部 gmail . 考虑到这些问题,请尝试以下方法:

    var str = "host,gmail,yahoo";
    var search = 'new@gmail.com';
    
    str.split(',').forEach(function(host) {
      if (search.indexOf(host) != -1) {
        console.log('Found');
      }
    });

    另外请注意,您可以显式地定义主机数组,避免 拆分() :

    var hosts = ['host', 'gmail', 'yahoo'];
    var search = 'new@gmail.com';
    
    hosts.forEach(function(host) {
      if (search.indexOf(host) != -1) {
        console.log('Found');
      }
    });
        2
  •  1
  •   ThS    6 年前

    作为 split 方法返回一个数组,您必须遍历该数组并检查是否匹配。

    下面是一个演示:

    // added gmail.com to the string so you can see more matched results(gmail and gmail.com).
    var str = "host, gmail, yahoo, gmail.com",
    search = 'new@gmail.com',
    splitArr = str.replace(/(\,\s+)/g, ',').split(','),
    /* the replace method above is used to remove whitespace(s) after the comma. The str variable stays the same as the 'replace' method doesn't change the original strings, it returns the replaced one. */
    l = splitArr.length,
    i = 0;
    
    for(; i < l; i++) {
      if(search.indexOf(splitArr[i]) > -1 ) {
       console.log('Found a match: "' + splitArr[i] + '" at the ' + i + ' index.\n');
      }
    }
        3
  •  0
  •   Alex Kudryashev    6 年前

    如您所见,没有子串 str 包含 search

    var str = "host, gmail, yahoo";
    var search = 'new@gmail.com';
    var res = str.split(', ').filter(function(el) {
      return search.indexOf(el) > -1;
    });
    console.log(res);
        4
  •  0
  •   Vishal Khemnar Digu    6 年前

    用此代码声明数组,这样就不需要用','分隔

    var str = new Array ("host","gmail","yahoo");
    

    要找到元素,请使用以下内容

        for (i = 0; i < str.length; ++i)
        {
             val = str[i];
             if (val.substring(0) === "gmail")
             {
                  res = val;
                  break;
             }
        }
        //Use res (result) here
    

    注:这是我的第一个答案,所以请原谅我,如果有一些错误。。。

        5
  •  0
  •   gaetanoM    6 年前

    为了达到你的结果,你需要提取 从搜索字符串。

    使用regex可以实现这一点:

    search.match( /\S+@(\S+)\.\S+/)[1]
    

    var str = "host, gmail, yahoo, qwegmail";
    var search = 'new@gmail.com';
    
    if( str.split(', ').indexOf(search.match( /\S+@(\S+)\.\S+/)[1]) > -1 ) {
        console.log(search + ': Found');
    } else {
        console.log(search + ': Not found');
    }
    
    
    search = 'asd@qwegmail.com';
    
    if( str.split(', ').indexOf(search.match( /\S+@(\S+)\.\S+/)[1]) > -1 ) {
        console.log(search + ': Found');
    } else {
        console.log(search + ': Not found');
    }