By JDT


2018-09-14 15:52:54 8 Comments

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?

3 comments

@Matee Gojra 2018-09-14 16:08:34

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);

@JDT 2018-09-19 15:56:02

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"

@Matee Gojra 2018-09-20 09:52:04

@JDT now check the edited answer above... now you can search by two words.

@JDT 2018-09-20 09:59:10

Does this solution work? Isn't broken.length actually 3 in this case?

@Matee Gojra 2018-09-22 05:13:32

your search criteria was about one-two words... so i designed according to your given criteria.

@JDT 2018-09-24 14:59:14

I know, I am saying in your example above you have broken.length == 2 but isn't it actually 3?

@Matee Gojra 2018-09-24 15:08:35

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]))

@Damian Czapiewski 2018-09-14 16:01:29

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;
}

@Tom 2018-09-14 15:57:19

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.

Related Questions

Sponsored Content

25 Answered Questions

[SOLVED] Generating random whole numbers in JavaScript in a specific range?

53 Answered Questions

[SOLVED] How do I include a JavaScript file in another JavaScript file?

23 Answered Questions

[SOLVED] In Node.js, how do I "include" functions from my other files?

43 Answered Questions

[SOLVED] How do I check if an array includes an object in JavaScript?

36 Answered Questions

[SOLVED] How do I check if a string contains a specific word?

74 Answered Questions

[SOLVED] How do I iterate over the words of a string?

  • 2008-10-25 08:58:21
  • Ashwin Nanjappa
  • 2056058 View
  • 2706 Score
  • 74 Answer
  • Tags:   c++ string split

13 Answered Questions

[SOLVED] How to do case insensitive search in Vim

2 Answered Questions

[SOLVED] Alternative to .includes()?

  • 2017-02-10 10:44:47
  • Cuckoo
  • 2646 View
  • 3 Score
  • 2 Answer
  • Tags:   javascript arrays

13 Answered Questions

[SOLVED] How do I convert a float number to a whole number in JavaScript?

  • 2009-02-27 20:15:07
  • mcherm
  • 770510 View
  • 889 Score
  • 13 Answer
  • Tags:   javascript syntax

2 Answered Questions

Sponsored Content