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?
Related Questions
Sponsored Content
44 Answered Questions
[SOLVED] How do I check if an array includes an object in JavaScript?
- 2008-10-25 22:14:40
- brad
- 2249705 View
- 3459 Score
- 44 Answer
- Tags: javascript arrays browser javascript-objects
36 Answered Questions
[SOLVED] How do I check if a string contains a specific word?
- 2010-12-06 13:14:05
- Charles Yeung
- 4066040 View
- 2667 Score
- 36 Answer
- Tags: php string substring contains string-matching
14 Answered Questions
[SOLVED] How do I convert a float number to a whole number in JavaScript?
- 2009-02-27 20:15:07
- mcherm
- 809344 View
- 928 Score
- 14 Answer
- Tags: javascript syntax
53 Answered Questions
[SOLVED] How do I include a JavaScript file in another JavaScript file?
- 2009-06-04 11:59:50
- Alec Smart
- 2608369 View
- 4477 Score
- 53 Answer
- Tags: javascript file import include
76 Answered Questions
26 Answered Questions
[SOLVED] Generating random whole numbers in JavaScript in a specific range?
- 2009-10-06 20:05:41
- zacharyliu
- 1030761 View
- 1637 Score
- 26 Answer
- Tags: javascript random integer
23 Answered Questions
[SOLVED] In Node.js, how do I "include" functions from my other files?
- 2011-04-26 23:56:59
- TIMEX
- 579935 View
- 789 Score
- 23 Answer
- Tags: javascript import header node.js
13 Answered Questions
[SOLVED] How to do case insensitive search in Vim
- 2010-02-18 09:17:00
- Haiyuan Zhang
- 386021 View
- 1439 Score
- 13 Answer
- Tags: search vim case-insensitive
2 Answered Questions
[SOLVED] Alternative to .includes()?
- 2017-02-10 10:44:47
- Cuckoo
- 3060 View
- 3 Score
- 2 Answer
- Tags: javascript arrays
2 Answered Questions
[SOLVED] Javascript - Search for whole words inside an array and create new array with coincidences
- 2016-04-30 14:50:14
- agustin
- 508 View
- -1 Score
- 2 Answer
- Tags: javascript arrays if-statement
3 comments
@Matee Gojra 2018-09-14 16:08:34
Try This Simplest Code without Regex
@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 ifwordToSearch = "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 addif(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
.@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.