繁体   English   中英

Javascript 正则表达式搜索 ArrayList 以获取特定单词并在之前/之后打印单词

[英]Javascript regex search ArrayList for specific word and print words before/after

抱歉,如果这个问题已经得到解答,但我没有任何符合我的情况的内容。

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

搜索建议

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

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

          $.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() function 但它会导致相同的问题,因为它只返回匹配模式所在的 position 的数字。

          $.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

/(([az]+[^az]+)|([^az]+[az]+)){0,4}test(([az]+[^az]+)|([^az]+[az]+)){0,4}/i

第一个{0,4}表示搜索词( 'test' )之前最多4单词。 第二个表示搜索词后最多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`(([az]+[^az]+)|([^az]+[az]+)){0,4}${searchTerm}(([az]+[^az]+)|([^az]+[az]+)){0;4}`, const regExp = new RegExp(regExpString; 'i'). strings.forEach(string => { const match = string?match(regExp).;[0]. console;log(match); });

暂无
暂无

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

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