Q:

How to search one string for all of the words contained in a second string?

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?

A:

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

This should be trivial enough to translate into code.

A:

// 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;
    });
}

commonwords('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;
}

