【问题标题】:Javascript regex search ArrayList for specific word and print words before/afterJavascript 正则表达式在 ArrayList 中搜索特定单词并在之前/之后打印单词
【发布时间】:2020-07-13 13:39:36
【问题描述】:

对不起,如果这个问题已经回答,但我没有任何符合我的情况的内容。

目前我正在尝试使用 Azure 认知搜索构建搜索建议功能。这工作得很好。但现在我尝试构建这样的东西:

建议应显示匹配模式,该模式在搜索栏中输入,前后各有几个词。如图所示。

我尝试构建一个数组,将完整内容拆分为单个单词并搜索模式。但这对我来说似乎很丑陋,因为我不知道如何反转数组并获取前后的单词。并将其编译为合适的字符串。

          $.ajax({
            url: "https://" + azSearchInstance + ".search.windows.net/indexes/" + azSearchIndex + "/docs?api-version=" + azApiVersion + "&search=" + text + '&$top=' + azSearchResults + '&api-key=' + azApiKey,
            method: 'GET'
          }).done(function (data) {
            // display results
            currentSuggestion2 = data[0];
            add(data);
            for(let i in data.value) {
              var content = data.value[i].content;
              var contentArray = content.split(' ');

              for(let word in contentArray) {
                if(contentArray[word] === text) {
                  console.log(contentArray[word]);
                }
              }
            }
            var render = Mustache.render(template, data);
            $(".search-suggest").html(render)
          });

我的第二次尝试是使用 indexOf() 函数,但它会导致同样的问题,因为它只返回匹配模式所在位置的数字。

          $.ajax({
            url: "https://" + azSearchInstance + ".search.windows.net/indexes/" + azSearchIndex + "/docs?api-version=" + azApiVersion + "&search=" + text + '&$top=' + azSearchResults + '&api-key=' + azApiKey,
            method: 'GET'
          }).done(function (data) {
            // display results
            currentSuggestion2 = data[0];
            add(data);
            for(let i in data.value) {
              var content = data.value[i].content;

              console.log(content.indexOf(text));
            }
            var render = Mustache.render(template, data);
            $(".search-suggest").html(render)
          });

我正在搜索一个正则表达式,它搜索模式并在模式前后打印 4-5 个单词。你们有人有想法吗?

提前谢谢你。

问候

OjunbamO

【问题讨论】:

  • 您可以创建一个正则表达式,其中包含前后 4-5 个单词的组。然后您可以使用该正则表达式进行搜索,最后操纵字符串以显示您想要的结果。但请注意,使用复杂的正则表达式可能会很快变慢。

标签: javascript regex azure search-suggestion


【解决方案1】:

/(([a-z]+[^a-z]+)|([^a-z]+[a-z]+)){0,4}test(([a-z]+[^a-z]+)|([^a-z]+[a-z]+)){0,4}/i

第一个 {0,4} 表示搜索词之前最多有 4 个字词 ('test')。第二个表示搜索词后最多4 个字。

它本来可以写成{4}(确切地说是4),但是当搜索词是第一个词时,这将不起作用。因为前面会有0 字样。

大部分复杂性是由于您的测试用例需要匹配{{testHelper}} 中的test 以及前后单词。

const searchTerm = 'test';
const strings = [
  'Hello World! And so, whenever you test-drive a new theme, make sure that you are buckled in.',
  'It is going to be a bumpy ride. In the below example there is a {{testHelper}} defined as an illustration.  This is for illustrative purposes.',
  'Some users have encountered the issue of duplicating the URL as TestName.github.io/Test for illegitamate uses.',
  'You do not have to, but if you want, you can click the Test connection button to make sure everything works as intended.',
];
const regExpString = String.raw`(([a-z]+[^a-z]+)|([^a-z]+[a-z]+)){0,4}${searchTerm}(([a-z]+[^a-z]+)|([^a-z]+[a-z]+)){0,4}`;
const regExp = new RegExp(regExpString, 'i');

strings.forEach(string => {
  const match = string.match(regExp)?.[0];
  console.log(match);
}); 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多