使用JavaScript忽略“a”,“the”等的stringsearch

我正在寻找像这样的东西:

if(magicSearch("hot pizza","We sell pizza that is really hot") {//Found!} 

如果两个词出现(任何顺序),我只想要一个“真”,我希望它在要search的文本中忽略“a”,“和”,“the”等等。

(我确定有更好的术语来解释我在找什么)

我已经看到各种数据库引擎支持这种types的文本search(例如Mongodb),但我需要一个简单的JavaScripttestingstring的方式。 我可以build立它,但感觉就像某个地方已经存在的东西。

格雷格

你可以在一个简单的循环中使用indexOf函数。

 //Use this if you don't need to find exact matches of words function magicSearch(needles, haystack) { var searchTerms = (needles instanceof Array) ? needles : [needles]; for(var i = 0; i < searchTerms.length; i++) { if(haystack.indexOf(searchTerms[i]) === -1) { return false; } } return true; } //Use this if you want to find exact matches of words function magicSearch2(needles, haystack) { var searchTerms = (needles instanceof Array) ? needles : [needles], haystackArray = haystack.split(' '), index, found = 0; for(var i = 0; i < haystackArray.length; i++) { index = searchTerms.indexOf(haystackArray[i]); if(index !== -1) { delete searchTerms[i]; found++; if(found = searchTerms.length) { return true; } } } return false; } if(magicSearch(["hot", "pizza"],"We sell pizza that is really hot") { console.log("FOUND"); } if(magicSearch2(["hot", "pizza"],"We sell pizza that is really hot")) { console.log("FOUND"); } 

如果你使用underscore.js ,你可以有一些相对简单的实现

 var stopwords_set = ["a", "and", "the" ]; var magicSearch = function (keywords, str) { var content_set = {}; var keywords_set = {}; _.each(keywords.split(' '), function(item){ keywords_set[item.toLowerCase()] = 1; }); _.each(str.split(' '), function(item){ content_set[item.toLowerCase()] = 1; }); //convert input to 2 sets excluding stop words content_set = _.without(_.keys(content_set), stopwords_set); keywords_set = _.without(_.keys(keywords_set), stopwords_set); //check the intersecion var value = _.intersection(content_set, keywords_set).length == keywords_set.length return value; } magicSearch("hot pizza","We sell pizza that is really hot");