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
4 Answers
Reset to default 7- Split the strings into arrays of words.
- For each word in
string A
, assignobj[word] = true;
. - For each word in
string B
, check ifobj[word] === true;
. If it does not, return false. - 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
版权声明:本文标题:javascript - How to search one string for all of the words contained in a second string? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1743950781a2567251.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论