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?


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?


@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){
        d1 = d.split(' ');
        result = d;
else if(broken.length == 2)
        var d1 = d.split(' ');
        if(d1.includes(broken[0]) && d1.includes(broken[1]))
            result = d;

@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] In Node.js, how do I "include" functions from my other files?

29 Answered Questions

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

55 Answered Questions

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

32 Answered Questions

[SOLVED] Split Strings into words with multiple word boundary delimiters

  • 2009-06-29 17:49:35
  • ooboo
  • 580084 View
  • 593 Score
  • 32 Answer
  • Tags:   python string split

76 Answered Questions

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

  • 2008-10-25 08:58:21
  • Ashwin Nanjappa
  • 2109018 View
  • 2830 Score
  • 76 Answer
  • Tags:   c++ string split

36 Answered Questions

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

46 Answered Questions

14 Answered Questions

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

  • 2009-02-27 20:15:07
  • mcherm
  • 865021 View
  • 972 Score
  • 14 Answer
  • Tags:   javascript syntax

2 Answered Questions

[SOLVED] Search for multiple words ignoring the order in Swift

13 Answered Questions

[SOLVED] How to do case insensitive search in Vim

Sponsored Content