如果这个问题已经回答了,我很抱歉,但我没有做任何与我的案例相匹配的事情。
目前,我正在尝试使用Azure Cognitive Search构建一个搜索建议功能。这是相当好的工作。但现在我尝试构建这样的东西:

建议应该显示匹配的模式,这是在搜索栏中输入前后的几个单词。也显示在图片中。
我试图构建一个数组,将完整的内容拆分到单个单词中并搜索模式。但这对我来说似乎非常难看,因为我不知道如何反转数组,并获得前后的单词。并将其编译为拟合字符串。
$.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
发布于 2020-07-23 09:00:52
/(([a-z]+[^a-z]+)|([^a-z]+[a-z]+)){0,4}test(([a-z]+[^a-z]+)|([^a-z]+[a-z]+)){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`(([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);
});
https://stackoverflow.com/questions/62877095
复制相似问题