admin管理员组

文章数量:1355684

I need to check a string A for a match based on whether it contains all of the words in another string B - in whatever order.

So, let's say string A is this:

one two three four five

And string B is one of these:

one two three // match!
one three two // match! (order doesn't matter)
one two six // NOT A MATCH! ('six' is not found in string A)
one two three four // match!
one two three four five // match!
one two three four five seven // NOT A MATCH! ('seven' is not found in string A)

How would I find a match between string A and string B only if every word in string B is found in string A (regardless of the order of the words in either string and regardless of whether string A contains additional words that are not found in string B)?

I don't know if jQuery has any special features that would help with this or whether I need to do it strictly with pure JavaScript?

I need to check a string A for a match based on whether it contains all of the words in another string B - in whatever order.

So, let's say string A is this:

one two three four five

And string B is one of these:

one two three // match!
one three two // match! (order doesn't matter)
one two six // NOT A MATCH! ('six' is not found in string A)
one two three four // match!
one two three four five // match!
one two three four five seven // NOT A MATCH! ('seven' is not found in string A)

How would I find a match between string A and string B only if every word in string B is found in string A (regardless of the order of the words in either string and regardless of whether string A contains additional words that are not found in string B)?

I don't know if jQuery has any special features that would help with this or whether I need to do it strictly with pure JavaScript?

Share Improve this question asked Nov 22, 2010 at 1:04 Oscar ReynesOscar Reynes 1232 silver badges4 bronze badges 1
  • you have String A and String B , take each word from string B and check if it is there in String A or not , if it is not there return false , it should be pretty simple. – kobe Commented Nov 22, 2010 at 1:53
Add a ment  | 

4 Answers 4

Reset to default 7
  1. Split the strings into arrays of words.
  2. For each word in string A, assign obj[word] = true;.
  3. For each word in string B, check if obj[word] === true;. If it does not, return false.
  4. Return true.

This should be trivial enough to translate into code.

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

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

monwords('one two three four five','one two nine');

// 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;
}
function pare(stringA, stringB) {
    // split for the words
    var aa = stringA.split(/\s+/), ab = stringB.split(/\s+/);
    var ha = {}, hb = {};

    // use a hash of the words
    for (var i = 0; i < aa.length; i ++) ha[aa[i]] = true;
    for (var j = 0; j < ab.length; j ++) hb[ab[j]] = true;

    // pare the two sets
    for (var k in hb) if (!ha.hasOwnProperty(k)) return false;
    return true;
}

Why not just create a Set of the words in String A (Object with true values in JavaScript), and check if it contains each word in String B?

How are you defining "word"? Is string.split("\\s+"); sufficient, or are you doing something fancier?

本文标签: javascriptHow to search one string for all of the words contained in a second stringStack Overflow