码迷,mamicode.com
首页 > Web开发 > 详细

[JS]在js中进行正则替换显示高亮处理中,重复替换问题,可以使用js自带的批量替换,则不会重复替换

时间:2018-01-18 13:35:28      阅读:178      评论:0      收藏:0      [点我收藏+]

标签:问题   event   regexp   数组   重复   word   批量替换   正则   div   

//js高亮显示

function highlight(){
    if($.trim($(‘#pscws‘).val()) != ‘‘){
        var url = $("#analysis_url").val();
        $.ajax({
            url:url,
            data:{words:$(‘#pscws‘).val()},
            dataType:‘json‘,
            type:‘POST‘,
            success:function (json) {
                if(json.status == 1 && json.data.length > 0){
                    var _key_word_str = json.data.join(‘|‘);//将要替换的词的数组组合成一个正则方式
                    $(".sEvent li strong>a,.sEvent li .tLDWords>em").each(function() {
                        var html=$(this).html();
                        var newhtml = html.replace(new RegExp(_key_word_str,‘gi‘), "<em class=\"wResult\">$&</em>");//执行js的批量替换,不会重复替换关键词
                        $(this).html(newhtml);
                    });
                }
            }
        });
    }
};

[JS]在js中进行正则替换显示高亮处理中,重复替换问题,可以使用js自带的批量替换,则不会重复替换

标签:问题   event   regexp   数组   重复   word   批量替换   正则   div   

原文地址:https://www.cnblogs.com/hhao321/p/8309359.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!