簡體   English   中英

如何將我的過濾器搜索輸入文本與我的 RegEx 匹配?

[英]How to match my filter search input text with my RegEx?

我有一個圖片庫,當我在搜索字段中鍵入 #cat(例如)時,它會過濾我的圖片標簽,這些標簽存儲在一個單獨的變量中,該變量從 JSON 文件中提取信息。

我有一個正則表達式,它不僅在標簽中搜索特定單詞,而且還搜索部分包含該單詞的標簽。 我需要它來避免部分包含該單詞的標簽。

HTML:

<div class="searchButton">
    <input type="text" id="inputValue" data-toggle="tooltip" placeholder="Search by name or #tag">
    <button id="searchBtn" onclick="goToPage(0,limit)" type="button">Search</button>
</div>

JS:

function goToItem(filter,imgIndex,count) {
    let imagesToDisplay = getImageArray(filter, imgIndex, count);
    const imgCount = getImagesCount();
    RenderPagingView(imgCount);
    renderImages(imagesToDisplay);
} 

function getImageArray(filter, imgIndexStart, numberOfImages) {
    let filter = $("#inputValue").val().toLowerCase();        
    searchByTag = filter[0] === "#";
    regexTag = new RegExp(filter.replace(/#/g, '').replace(/,/, '|').match(/\W*(`//how to place the input field of the filter variable here ,so to watch for that word only ??`)\W*/g));
    regexTitle = new RegExp(filter.replace(/,/g, '|'));
    let filteredArrayPhotos=[];

    tmpFiltered = arrayPhotos.filter(function searchFilter (image){
    return searchByTag ? regexTag.test(image.tag) : regexTitle.test(image.title.toLowerCase());
}

例如,如果 For 循環有更好的方法,它也將適合。

我改變了:

regexTag = new RegExp(filter.replace(/#/g, '').replace(/ /g, '').replace(/,/, '|') );

至:

regexTag = new RegExp(('\\b' + filter + '\\b').replace(/#/g, '').replace(/ /g, '').replace(/,/, '|') );

現在一切正常。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM