【问题标题】:Javascript -- How to find and replace text matched via nested array list?Javascript——如何查找和替换通过嵌套数组列表匹配的文本?
【发布时间】:2014-01-18 07:51:32
【问题描述】:

我正在尝试在文档中的所有 <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]);
                    }
                }
            });

这是我的代码的样子,但我仍然无法执行任务...请帮助我。

【问题讨论】:

    标签: javascript jquery multidimensional-array


    【解决方案1】:

    @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 在 for 语句中多次读取。如果您想使用@BlackShape,您可以只读取一次并分配给局部变量并在 for 循环语句中使用它。

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

    【讨论】:

    • 为什么没有效率? $.each() 不使用 for 循环吗? github.com/jquery/jquery/blob/master/src/core.js#L298
    • 每次在for循环中读取数组长度。这是好方法吗?
    • 对不起,我的意思不同。
    • 我明白你的意思,但如果性能是关键,我不会使用$.each()
    • 为什么?你认为 $.each 的性能不如 for 循环?
    【解决方案2】:

    主要问题是您没有重置 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
        });
    });
    

    【讨论】:

    • 如果我想在&lt;p&gt; 中搜索,如果它包含多个错误,例如:“DBCONERROR01,DBCONERROR04 是/曾经是/曾经是由于哪个任务无法执行而导致的原因!”
    • @user3149111 好吧,我们使用for 循环来遍历每个p 元素的整个数组,你试过了吗?它失败了吗?如果是!,您能否在jsfiddle.net 上重现该问题?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-01
    • 1970-01-01
    • 2013-07-26
    • 1970-01-01
    相关资源
    最近更新 更多