V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐关注
Meteor
JSLint - a JavaScript code quality tool
jsFiddle
D3.js
WebStorm
推荐书目
JavaScript 权威指南第 5 版
Closure: The Definitive Guide
CupTools
V2EX  ›  JavaScript

How do you check if a string contains ALL strings from another array?

  •  
  •   CupTools · 2016-04-25 15:35:59 +08:00 · 2353 次点击
    这是一个创建于 2927 天前的主题,其中的信息可能已经有所发展或是发生改变。

    Say I have some strings,

    var string1 = "hey this is jack and michael";
    var string2 = "hey this is jack and jenny";
    

    And I have an array of strings:

    var arr = ['jack', jenny'];
    

    How do you write a function so that:

    fn(string2, arr); // true
    fn(string1, arr); // false
    

    Kind of like String.indexOf() or regex with a|b|c, but no individually.

    第 1 条附言  ·  2016-04-25 16:13:12 +08:00

    渣渣...

    function containsAll(haystack, needles){
    	for (var i = 0; i < needles.length; i++){
    		if (haystack.indexOf(needles[i]) === -1)
    		return false;
    	}
    	return true;
    }
    
    10 条回复    2016-04-25 22:21:10 +08:00
    ershisi
        1
    ershisi  
       2016-04-25 15:49:37 +08:00   ❤️ 1
    虽然不知道为什么要用英文。你的数组缺少一个闭合。
    另外最简单的办法,如果你的字符串都是以空格分隔的。那就把第一个 string 参数分割成数组。然后再去判断。用不着正则的感觉。。。
    Magic347
        2
    Magic347  
       2016-04-25 15:53:25 +08:00   ❤️ 1
    python 版:

    def fn(str, arr):
    ____return all(x in str for x in arr)
    CupTools
        3
    CupTools  
    OP
       2016-04-25 16:11:05 +08:00
    @ershisi 中文太差 表达不清
    spance
        4
    spance  
       2016-04-25 16:27:52 +08:00
    这个其实就是多模式匹配,比较好的方法是使用 W-M 或者 A-C 算法可以做到 O(m+n),或者没啥要求或者数量较小就像最后那个 for 里面逐个查找也可以。
    jyyyxy
        5
    jyyyxy  
       2016-04-25 17:13:18 +08:00
    java 版 BiPredicate<String, List<String>> fun = (str, arr) -> arr.stream().allMatch(str::contains);
    xjp
        6
    xjp  
       2016-04-25 19:11:33 +08:00
    arr.every((s) => string1.contains(s));
    murmur
        7
    murmur  
       2016-04-25 19:16:48 +08:00
    AC 自动机 应该有 java 版的
    zhujinliang
        8
    zhujinliang  
       2016-04-25 19:25:28 +08:00
    javascript:

    function containsAll(haystack, needles) {
    return !!haystack.match(new RegExp('('+needles.join('|')+')'));
    }
    pollow
        9
    pollow  
       2016-04-25 19:48:59 +08:00
    我还以为进来能看到有人讨论 AC 自动机,没想到一群写循环的……
    wsdjeg
        10
    wsdjeg  
       2016-04-25 22:21:10 +08:00
    VIML

    let s:str1 = "hey this is jack and michael"
    let s:str2 = "hey this is jack and jenny"
    let s:list = ['jack', 'jenny']

    fu! s:checkstr(str,list)
    if 0 < index(split(a:str,' '),a:list[0]) && index(split(a:str,' '),a:list[0]) < index(split(a:str,' '),a:list[1])
    return "true"
    else
    return "false"
    endif
    endf

    echom s:checkstr(s:str1,s:list)
    echom s:checkstr(s:str2,s:list)
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   877 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 32ms · UTC 22:11 · PVG 06:11 · LAX 15:11 · JFK 18:11
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.