Javascript - 如何查找和替换通过嵌套数组列表匹配的文本?

时间:2013-12-31 11:01:29

标签: javascript jquery multidimensional-array

我正在尝试在文档中的所有<p>标记中查找文本,并替换文本(如果该文本存在于search_array列表中):

search_array=[
                ['DBCONERROR01','Unable to Connect Database Server'],
                ['DBCONERROR02','Error Occured With DataBase Connection'],
                ['DBCONERROR03','Unable to Communicate with Data Base'],
                ['DBQRYERROR01','Invalid Query OR DataBase Error'],
                ['DBCONERROR04','Connection Lost with Database'],
                ['DBQRYERROR02','DataBase Query Failed'],
                ['DBQRYERROR03','Invalid to Wrong Sql Query'],
                ['TARIFERROR01','No Rates Found for Tariff'],
                ['AUTHSERROR01','Authentications not Found'],
                ['SWICHERROR01','Unable to Find Switch Details'],
                ['IOPRMERROR01','File Permission Error'],
                ['IOPRMERROR01','IO Error with operation System'],
                ['IOPRMERROR01','File Handling Error - Unable to Communicate with IO'],
                ['OPSSHERROR01','Unable to SSH switch - Connection Error'],
                ['OPSSHERROR02','SSH to Switch Failed'],
                ['OPSSHERROR03','Unable to Copy Scripts to Switch'],
                ['OPSSHERROR04','Unable to Execute Script on Switch'],
                ['JSONPERROR01','Unable to Parse Json'],
                ['TARIFERROR02','No Entry Found'],
                ['TARIFERROR03','Unable to Push Rates TO SBC'],
                ["DoesNotExist('Email does not exist.',)",'No Emails Received']
            ]
        $( document ).ready(function() {
            for(var i=0; i<search_array.length+1; i++)
                {
                    console.log(i);
                    console.log(search_array.length);
                    for(var j=0; j<search_array[i].length; j++)
                    {
                        var str = $("p").text();
                        console.log(str[0]);
                        str.replace(search_array[j], search_array[j+1]);
                    }
                }
            });

这是我的代码看起来像,但我仍然无法执行任务......请帮助我。

2 个答案:

答案 0 :(得分:3)

主要问题是你不重置p元素'textContent,同样.replace()方法保持原始字符串不变。您可以使用text()方法回调函数,对集合中的每个选定元素执行一次回调:

// var search_array = [ ... ];
$(document).ready(function() {
    $('p').text(function(_, text) {
       for (var i = 0; i < search_array.length; i++) {
           text = text.replace(search_array[i][0], search_array[i][1]);
       }; 
       return text; // return the modified textContent
    });
});

答案 1 :(得分:0)

@BlackShape使用每个函数回答一点修改后的版本:

// var search_array = [ ... ];
$(document).ready(function() {
    $('p').text(function(_, text) {
       $.each(search_array, function(index){
          text = text.replace(search_array[index][0], search_array[index][1]);    
       }); 
       return text; // return the modified textContent
    });
});

@ BlackShape方法的另一个缺点是search_array.length在语句中多次读取。如果你想使用@BlackShape,你只能读一次并分配给局部变量并在for循环语句中使用它。

for (var i = 0; i < search_array.length; i++)