1

I have created a search mechanism that searches through an array of strings for an exact string match, however I want it to be a bit more intuitive.

I can also get it to search for a string within the string (for example chicken in grilled chicken - however the issue is this allows users to type ken or ill and it returns grilled chicken.

I would like it to return if I typed in chicken or grilled.

Does anyone have any suggestions on how to have a more intuitive search mechanism?

EDIT:

The correct answer below worked when typing 1 word and it would search all individual words in a string. However, I realised it fails when you search with 2 words (as it only searches each string word individually).

I solved this by adding || search == string to the if to include not just individually word matches but whole string matches.

However I am still having an issue with it either searching for:

Whole string matches OR Matches with individual words.

This means it fails when search = green cup and string = big green cup. Is there a way to solve this by cutting for collections to search within? Perhaps something similar to:

string.split(' ') but to also include big green, green cup to the array also?

JDT
  • 965
  • 2
  • 8
  • 20

3 Answers3

5

Try This Simplest Code without Regex

var data = ["first string1 is here", "second string2 is here", "third string3 is here"];
    var wordToSearch = "string2 is thanks";
    var broken = wordToSearch.split(' ');
    var result = 'not found';
    if(broken.length == 1){
    data.forEach(function(d){
        d1 = d.split(' ');
    if(d1.includes(wordToSearch))
        result = d;
});
}
else if(broken.length == 2)
{
    data.forEach(function(d){
        var d1 = d.split(' ');
        if(d1.includes(broken[0]) && d1.includes(broken[1]))
        {
            result = d;
        }
    });
}
alert(result);
Dev Matee
  • 5,525
  • 2
  • 27
  • 33
  • Hi, I have just realised that this missed part of the functionality I am missing - can you help solve it? This indeed does a 1 word match for whole words in a string, however I want it to also search for the whole phrase (so return true if `wordToSearch = "first string1"`. With that, I would also like phrases with 3 words to match if 2 match (return true if `wordToSearch = "big cup"` `string = "green big cup"` – JDT Sep 19 '18 at 15:56
  • @JDT now check the edited answer above... now you can search by two words. – Dev Matee Sep 20 '18 at 09:52
  • Does this solution work? Isn't `broken.length` actually 3 in this case? – JDT Sep 20 '18 at 09:59
  • your search criteria was about one-two words... so i designed according to your given criteria. – Dev Matee Sep 22 '18 at 05:13
  • I know, I am saying in your example above you have `broken.length == 2` but isn't it actually 3? – JDT Sep 24 '18 at 14:59
  • actually you asked to match one word or two... if you want to compare exactly 3 words then we can do `broken.length == 3` and in IF condition we have to add `if(d1.includes(broken[0]) && d1.includes(broken[1]) && d1.includes(broken[2]))` – Dev Matee Sep 24 '18 at 15:08
2

I'd use RegExp with word boundary anchor - \b.

function search(query, arr) {
    var res  = [];
    var re = new RegExp('\\b' + query + '\\b');
    arr.forEach(function (item) {
        if (re.test(item)) res.push(item);
    });
    return res;
}
1

It sounds like you only want to search by whole words, if that's the case, you could split the string by the space character and then search through the resultant array for matches.

Tom
  • 2,734
  • 2
  • 22
  • 39