查找一个搜索词数组是否在我的内容中也包含一个字符串的最快方法?

时间:2022-12-11 01:34:16

I am trying to create a search box where as the user types in their search terms, it constantly assesses whether the search terms are in my content on the fly.

我正在尝试创建一个搜索框,当用户在搜索词中键入时,它会不断地评估搜索词是否在我的内容中。

I have an array of search terms and phrases, like such:

我有一系列的搜索词和短语,比如:

["home", "and", "garden", "weeding a flower patch", "best tools"]

I also have a string that contains the content I'm searching in, such as this:

我还有一个字符串,它包含我正在搜索的内容,例如:

"Using your hands to dig your flower holes brings you closer to the earth."

In Javascript, I need to see if any of the search terms are in any portion of this string. I don't need to know which terms match or where, only that somewhere in that string is at least one instance of one of my search terms. It's very important though that it also finds partial matches in the content string. For example, in the example above, it should return true, because the word "hands" contains the word "and" from my search terms. It's also important that it be done in the fastest, least computationally heavy way, since I will be calling this function thousands of times in a row. How can I best do this?

在Javascript中,我需要查看是否有任何搜索项在该字符串的任何部分中。我不需要知道哪个词匹配在哪,只需要知道在那个字符串中至少有一个我的搜索词的实例。这很重要,但它也在内容字符串中找到部分匹配。例如,在上面的例子中,它应该返回true,因为单词“hands”包含了单词“和”。同样重要的是,它要以最快的、最少的计算量完成,因为我将连续调用这个函数数千次。我该怎么做呢?

3 个解决方案

#1


4  

Somewhat similar to Almog Baku's solution is how I would do it:

与Almog Baku的解决方案有些相似的是,我将如何做:

var makeSearcher = function(terms) {
    var regex = new RegExp(terms.join("|"));
    return function(str) {
        return regex.test(str);
    };
};

var searcher = makeSearcher(["home", "and", "garden", 
                             "weeding a flower patch", "best tools"]);

searcher("Using your hands to dig ...");  //=> true
searcher("Using your fingers to dig ..."); //=> false

This technique will only work if your search terms do not contain any characters that are special in regular expressions. But I think it's probably fairly efficient, assuming that your search terms are relatively fixed.

只有当搜索术语不包含正则表达式中任何特殊的字符时,这种技术才有效。但我认为它可能是相当有效的,假设你的搜索词是相对固定的。

#2


2  

My suggestion is simple:

我的建议很简单:

For best parctice(=faster!) we should build a regrular expression, than we can simply use the search function.

为了实现最佳的分配(=更快!),我们应该构建一个regrular表达式,而不是简单地使用search函数。

String.prototype.arraySearch = function(terms) {
    var regex = new RegExp(terms.join("|"));
    return regex.test(this);
}

than you can just do search on your strings:

而不是搜索你的字符串:

var text = "Using your hands to dig your flower holes brings you closer to the earth.";
var keywords = ["home", "and", "garden", "weeding a flower patch", "best tools"];
console.log(text.arraySearch(keywords);

//or even
"Using your hands to dig your flower holes brings you closer to the earth.".arraySearch(["home", "and", "garden", "weeding a flower patch", "best tools"]);

#3


0  

You can loop through your search terms and use JavaScript's indexOf function to check if the search term exists in the string like this:

你可以循环你的搜索词,并使用JavaScript的indexOf函数来检查搜索词是否存在于这样的字符串中:

function(needles, haystack){
    var x;
    for(x = 0; x < needles.length; x++){
        if(haystack.indexOf(needles[x]) > -1){
            return true;
        }
    }
    return false;
}

#1


4  

Somewhat similar to Almog Baku's solution is how I would do it:

与Almog Baku的解决方案有些相似的是,我将如何做:

var makeSearcher = function(terms) {
    var regex = new RegExp(terms.join("|"));
    return function(str) {
        return regex.test(str);
    };
};

var searcher = makeSearcher(["home", "and", "garden", 
                             "weeding a flower patch", "best tools"]);

searcher("Using your hands to dig ...");  //=> true
searcher("Using your fingers to dig ..."); //=> false

This technique will only work if your search terms do not contain any characters that are special in regular expressions. But I think it's probably fairly efficient, assuming that your search terms are relatively fixed.

只有当搜索术语不包含正则表达式中任何特殊的字符时,这种技术才有效。但我认为它可能是相当有效的,假设你的搜索词是相对固定的。

#2


2  

My suggestion is simple:

我的建议很简单:

For best parctice(=faster!) we should build a regrular expression, than we can simply use the search function.

为了实现最佳的分配(=更快!),我们应该构建一个regrular表达式,而不是简单地使用search函数。

String.prototype.arraySearch = function(terms) {
    var regex = new RegExp(terms.join("|"));
    return regex.test(this);
}

than you can just do search on your strings:

而不是搜索你的字符串:

var text = "Using your hands to dig your flower holes brings you closer to the earth.";
var keywords = ["home", "and", "garden", "weeding a flower patch", "best tools"];
console.log(text.arraySearch(keywords);

//or even
"Using your hands to dig your flower holes brings you closer to the earth.".arraySearch(["home", "and", "garden", "weeding a flower patch", "best tools"]);

#3


0  

You can loop through your search terms and use JavaScript's indexOf function to check if the search term exists in the string like this:

你可以循环你的搜索词,并使用JavaScript的indexOf函数来检查搜索词是否存在于这样的字符串中:

function(needles, haystack){
    var x;
    for(x = 0; x < needles.length; x++){
        if(haystack.indexOf(needles[x]) > -1){
            return true;
        }
    }
    return false;
}