簡體   English   中英

在多維數組上顯示多個搜索結果

[英]Display more than one search result on Multidimensional Array

我想在多維數組中搜索時遇到問題。 我在文本區域內有一個字符串

<textarea id='alltemplatememe'>
    ['2','makan lagi','kwik-templatememe-20171208215155.jpg'],['1','apapun','kwik-templatememe-20171208215124.jpg']
</textarea>

我將把textarea中的數據更改為數組,例如下面的函數

function meme_template_search(){
    var keyword=$("#keyword_template").val();
    var template=$("#alltemplatememe").val();
    var resultsame="";
    template=eval("[" + template + "]");
    for(var i=0; i<template.length; i++){
        result=template[i][1].search(new RegExp(keyword, "i"));
        if(result=="1"){
            resultsame+=template[i][1]+", ";
        }
        result="";
    }
    alert(resultsame);
}

我已經嘗試了上面的代碼,但是問題是上面的代碼只是顯示一個結果

當我使用關鍵字“ a”進行搜索時,應該為兩個,但是我制作的函數僅顯示一個結果

您將結果限制為僅此一行

if(result=="1"){

結果您的關鍵字在字符串中出現的位置。

即:

Search 'a' in 'Apple' -> 0        | Search 'to' in 'Potato' -> 4

如果您的關鍵字未出現在字符串中,則結果為-1

因此,將您的行更改為:

if (result !== -1) {

匹配字符串時為true。

有關更多詳細信息,請參見此處: JavaScript RegExp對象

暫無
暫無

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

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