简体   繁体   English

在较大的字符串中搜索多个子字符串

[英]Search for multiple substrings within a larger string

I'm working on building a document library, in which each of the "cards" in our catalog are a string that looks like this: 我正在构建一个文档库,其中目录中的每个“卡片”都是一个如下所示的字符串:

'<div class="cardBox col-lg-4 col-md-6 col-sm-12"><div class="card" style="margin-bottom: 1em;"><img class="card-img-top" src="' + data.values[n][5] + '" alt="Card image cap"><div class="card-body"><h5 class="card-title">' + data.values[n][0] + '</h5><h6 class="doctype card-subtitle mb-2 text-muted">' + data.values[n][3] + '</h6><p class="card-text"><span class="font-weight-bold">Document Part #:</span> ' + data.values[n][1] + '<br /><span class="font-weight-bold">Rev:</span> ' + data.values[n][4] + '<br /><span class="font-weight-bold">Language:</span> <span class="language">' + data.values[n][10] + '</span><br /><span class="font-weight-bold">Customer:</span> <span class="customer">' + data.values[n][2] + '</span><br /><span class="font-weight-bold">Date Updated:</span> ' + data.values[n][11] + '</p><a href="' + data.values[n][7] + '" class="card-link" target="_blank">View Document</a><a href="' + data.values[n][6] + '" class="card-link" target="_blank">Download</a></div></div></div>'

I've built a search function that looks through this string for the text input into the search field, that looks like this: 我建立了一个搜索功能,该功能通过该字符串查找输入到搜索字段中的文本,如下所示:

function searchByTitle() {
        var filter, card, title, i
        filter = document.getElementById("searchText").value.toUpperCase().split(" ");
        card = document.getElementById("content").getElementsByClassName("cardBox");
        console.log(filter);

        for (h = 0; h <= filter.length; h++) {
            for (i = 0; i < card.length; i++) {
                if (card[i].innerHTML.toUpperCase().includes(filter[h]) == true) {
                    card[i].style.display = "";
                } else {
                    card[i].style.display = "none";
                }
            }
        }
    }

The problem is, if you search for a set of substrings (separated by spaces in the input field), if the substrings are NOT in sequential order, the page shows no documents that match the search string. 问题是,如果您搜索一组子字符串(在输入字段中用空格分隔),并且这些子字符串不是按顺序排列的,则该页面不会显示与搜索字符串匹配的文档。

So, if I were to search for: freezer widget thingy , if those three substrings do not appear in that exact order within the card, nothing will be displayed. 因此,如果我要搜索: freezer widget thingy ,如果这三个子字符串在卡中的排列顺序不正确,则不会显示任何内容。

Basically: how can I search through a string for substrings that appear in any arbitrary order within the larger string? 基本上:如何在字符串中搜索在较大字符串中以任意顺序出现的子字符串?

EDIT: Tried to loop through both the text I'm searching through, as well as the array of search terms, but with no luck. 编辑:试图遍历我正在搜索的文本以及搜索词的数组,但是没有运气。

EDIT 2: Tried to go about looping things with the above, modified code. 编辑2:试图去循环与上面的修改后的代码。 Now my search isn't returning anything when I type into the input field. 现在,当我在输入字段中键入内容时,搜索不返回任何内容。 What's going on here? 这里发生了什么?

您可以使用此card[i].innerHTML.toUpperCase().includes(...filter.split(' '))代替card[i].innerHTML.toUpperCase().includes(...filter.split(' '))

I would suggest you to use regular expression for such task: 我建议您对此类任务使用正则表达式:

if (searchText.match(/(^(?=.*freez)(?=.*widget).*$)/g)){
   card[i].style.display = ""; 
} else {
   card[i].style.display = "none";
}

That would match for example: 例如,这将匹配:

freezer widget
widget freezer
freezer widget thingy

This reg exp mechanism (?=.*something) uses lookarounds, more http://www.rexegg.com/regex-disambiguation.html#lookarounds 此reg exp机制(?=.*something)使用环视,更多http://www.rexegg.com/regex-disambiguation.html#lookarounds

That reg exp can be easily expanded to look for more lookaround groups. 该reg exp可以轻松扩展以寻找更多的环顾小组。 Simply add new words you are looking for: 只需添加您要查找的新单词:

/(^(?=.*freez)(?=.*widget)(?=.*deer)(?=.*warlock).*$)/g

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

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