简体   繁体   English

JavaScript的基本搜索功能

[英]Basic search functionality with JavaScript

I'm looking for a basic search functionality with JavaScript. 我正在寻找JavaScript的基本搜索功能。

The Scenario : The user enters a single or multiple words, and hit a button. 场景 :用户输入单个或多个单词,然后单击按钮。 JavaScript looks up in an array of strings for items that probably relates to the entered search sentence. JavaScript查找可能与输入的搜索句子相关的项目的字符串数组。

The only function I'm aware of right now is "string.search" which returns the position of the string you are searching for. 我现在唯一知道的函数是“string.search”,它返回你要搜索的字符串的位置。 This is good, but not for all cases. 这很好,但不适用于所有情况。 Here are a few examples the search function should cover 以下是搜索功能应涵盖的一些示例

Let's assume I have the following string: "This is a good day" in my array. 我们假设我的数组中有以下字符串:“这是一个美好的一天”。 The following search terms should return true when tested against my search function. 在针对我的搜索功能进行测试时,以下搜索字词应返回true。

  • Search Term 1: This a good day 搜索术语1:这是美好的一天
  • Search Term 2: This day 搜索术语2:这一天
  • Search Term 3: This was good 搜索术语3:这很好
  • Search Term 4: good dy - the user made a typo - 搜索条件4:好dy - 用户输了一个错字 -

So nothing particular or specific. 所以没什么特别或具体的。 Just a basic search functionality that predicts (at a low level, and language agnostic) if the search term related to the strings in the tested array. 只是一个基本的搜索功能,如果搜索项与测试数组中的字符串相关,则预测(在低级别,语言不可知)。

Was the last a typo for 'day'? 最后一个是'天'的错字吗?

If not, you could simply split the search sentence, as well as the original string using the split() function. 如果没有,您可以使用split()函数简单地拆分搜索句子以及原始字符串。

Then you would iterate over the search words and for each make sure they appear in the source string. 然后,您将迭代搜索词,并确保它们出现在源字符串中。 As soon as you don't find the word, you stop the search. 一旦找不到该单词,就会停止搜索。

This is assuming that all the search words should be AND'ed, not OR'ed. 这假设所有搜索词应该是AND,而不是OR。

Does that help? 这有帮助吗?

This is not as simple as one would think. 这并不像人们想象的那么简单。 We're talking fuzzy matching and Levenshtein distance / algorithm. 我们正在讨论模糊匹配和Levenshtein距离/算法。

See this past question: Getting the closest string match 查看过去的问题: 获得最接近的字符串匹配

I guess what you are looking for is a pattern matching based live search similar to finite-state-automata-like (FSA) searching: 我想你要找的是基于模式匹配的实时搜索,类似于有限状态自动机(FSA)搜索:

This link shows an example that'll allow you to search case-insensitively: 此链接显示了一个示例 ,它允许您不区分大小写地搜索:

Example: Array contains 'This is a good day' 示例:数组包含'这是美好的一天'

Searching for any (or all) of the following is valid: 搜索以下任何(或全部)有效:

  • THis a Day 这是一天
  • Thagd ( Th is a g oo d day) Thagd(Th OO d天)
  • good dy -intended typo- 好的dy -intended typo-

etc. 等等

A case-sensitive (albeit not perfect FSA based) version can be found here There is also one by John Resig but I don't have a link to his demo but it'd be worth looking at - it's a javascript/jquery port of the first link I mentioned. 一个区分大小写(虽然不是完美的基于FSA)版本可以在这里找到一个由约翰Resig也有一个,但我没有他的演示的链接,但它值得一看 - 这是一个javascript / jquery端口我提到的第一个链接。

Hope this helps! 希望这可以帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM