且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在一个字符串中搜索第二个字符串中包含的所有单词?

更新时间:2022-11-16 17:43:08

//如果客户端具有数组方法 every ,则此方法非常有效-

// If the client has the array method every, this method is efficient-

function commonwords(string, wordlist){
    string= string.toLowerCase().split(/\s+/);
    wordlist= wordlist.toLowerCase().split(/\s+/);
    return wordlist.every(function(itm){
        return string.indexOf(itm)!= -1;
    });
}

常用词(一二三四有五",一二有九");

//如果您希望任何客户端在没有特殊功能的情况下进行处理, 您可以解释"高级数组方法-

// If you want any client to handle it without a special function, you can 'explain' the advanced array methods-

Array.prototype.every= Array.prototype.every || function(fun, scope){
    var L= this.length, i= 0;
    if(typeof fun== 'function'){
        while(i<L){
            if(i in this && !fun.call(scope, this[i], i, this)) return false;
            ++i;
        }
        return true;
    }
    return null;
}
Array.prototype.indexOf= Array.prototype.indexOf || function(what, i){
    i= i || 0;
    var L= this.length;
    while(i< L){
        if(this[i]=== what) return i;
        ++i;
    }
    return -1;
}