简体   繁体   English

在javascript中使用正则表达式进行基本搜索排名

[英]basic search ranking with regex in javascript

Currently I am using the below for search. 目前,我正在使用以下内容进行搜索。 I assume each and every term the user types must appear at least once in the article. 我假设用户键入的每个术语在文章中必须至少出现一次。 I use the match method with regex 我在正则表达式中使用match方法

^(?=.*one)(?=.*two)(?=.*three).*$

with g , i , and m gim

At the moment I use matches.length to count the number of matches, but the behavior is not as expected. 目前,我使用matches.length来计算匹配的数量,但是行为并不符合预期。 example: " one two three. one two three " would give me 2 matches, but it should really be 6. 例如:“ one two three. one two three ”会给我2场比赛,但实际上应该是6场。

If I do something like 如果我做类似的事情

(one|two|three)

then I do get 6 matches, but if I have the data: 那么我确实得到了6个匹配项,但是如果我有数据:

"one two. one two"

I get 4 matches, when in reality I want it to be 0, since not every word appears at least once. 我得到4个匹配项,实际上我希望将其设为0,因为并非每个单词都至少出现一次。 I could do the first regex to check if there's at least one "match". 我可以做第一个正则表达式来检查是否至少有一个“匹配项”。 If there is, I would subsequently use the second regex to count the real number of matches, but this would make my program run much slower than it already is. 如果有的话,我随后将使用第二个正则表达式来计算实际的匹配数,但这会使我的程序运行得比现在慢得多。 Doing this regex against 2500 json articles takes anywhere from 60 to 120 seconds as it is. 对2500个json文章执行此正则表达式需要60到120秒。

Any ideas on how to make this faster or better? 关于如何使其更快或更佳的任何想法? Change the regex? 更改正则表达式? Use search or indexOf instead of matches? 使用search或indexOf代替匹配项?


note: I'm using lawnchair db for local persistance and jquery. 注意:我正在使用grasschair db进行本地持久性和jquery。 I package the code for phonegap and as a chrome packaged app. 我将用于phonegap的代码打包为Chrome打包的应用程序。

var input = '...';
var match = [];
if (input.match(/^(?=.*\bone\b)(?=.*\btwo\b)(?=.*\bthree\b)/i)) {
  match = input.match(/\b(one|two|three)\b/ig);
}

Test this code here . 在此处测试此代码。

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

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