2

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?

dallin
  • 7,138
  • 1
  • 29
  • 36

3 Answers3

5

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

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.

Scott Sauyet
  • 37,179
  • 4
  • 36
  • 82
2

My suggestion is simple:

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

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"]);
Almog Baku
  • 821
  • 1
  • 9
  • 22
  • if you want to escape special characters to allow using them on your search- you should have to add them manually within a loop as I did in the first edition and escape them like it done here http://stackoverflow.com/questions/3446170/escape-string-for-use-in-javascript-regex – Almog Baku Sep 06 '13 at 10:37
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:

function(needles, haystack){
    var x;
    for(x = 0; x < needles.length; x++){
        if(haystack.indexOf(needles[x]) > -1){
            return true;
        }
    }
    return false;
}
kenttam
  • 740
  • 4
  • 9
  • But he wants an impartial match, so that's not a complete solution. – Joe Simmons Sep 06 '13 at 03:53
  • My solution, though may not be as efficient as the other with regular expression, does still find partial matches with indexOf. – kenttam Sep 06 '13 at 04:38
  • +1 It may not be as fast as the other solutions, but it does completely solve the problem. I did look into it and I believe the regex answers are faster, but I think it's good to have as an alternate and striaghtforward answer in case regex won't work for someone for some reason. – dallin Sep 06 '13 at 19:02
  • Sorry, I misinterpreted your code. You should probably make it clear that you're using String's indexOf, not Array's. – Joe Simmons Sep 07 '13 at 06:40