Javascript RegExp用于匹配不属于HTML标记的文本

时间:2013-07-25 10:26:23

标签: javascript html regex replace lookbehind

我试图找到一种方法来突出HTML中的一些文本。提供以下HTML:

<div>This text contains matching words like word1 and word2 and xyzword1xyz and word2xyz and xyzword2</div>

应该由<span>包围的单词列表是:

var array = ['word1','word2', 'word1word3'];

我目前的Javascript:

$.each(array , function(index, elem){
            if(elem.length<3 || elem === "pan" || elem === "spa" || elem === "span")return true;             
            var re = new RegExp(""+elem+"(?=([^']*'[^']*')*[^']*$)","gi");
            returnString = returnString.replace(re, "<span class='markedString colorword1orword2'>$&</span>");                
});

结果div看起来像:

<div>This text contains matching words like <span class='markedString colorword1orword2'>word1</span> and <span class='markedString colorword1orword2'>word2</span> and xyz<span class='markedString colorword1orword2'>word1</span>xyz and <span class='markedString colorword1orword2'>word2</span>xyz and xyz<span class='markedString colorword1orword2'>word2</span> and finally <span class='markedString colorword1orword2'><span class='markedString colorword1orword2'>word1</span>word3</span></div>

由于class='markedString colorword1orword2'中当前的regexp everthing不匹配。

问题: 如果数组看起来像

var array = ['word1','word2', 'class'];

我最终会以

结束
<div>This text contains matching words like <span <span class='markedString colorword1orword2'>class</span>='markedString colorword1orword2'>word1</span> and <span <span class='markedString colorword1orword2'>class</span>='markedString colorword1orword2'>word2</span> and xyz<span <span class='markedString colorword1orword2'>class</span>='markedString colorword1orword2'>word1</span>xyz and <span <span class='markedString colorword1orword2'>class</span>='markedString colorword1orword2'>word2</span>xyz and xyz<span <span class='markedString colorword1orword2'>class</span>='markedString colorword1orword2'>word2</span> and finally <span <span class='markedString colorword1orword2'>class</span>='markedString colorword1orword2'><span <span class='markedString colorword1orword2'>class</span>='markedString colorword1orword2'>word1</span>word3</span></div>

此示例以某种方式构建,因此可能存在其他可能位于HTML标记中的单词。

我需要一种模拟regexp-lookbehind的方法,以便我可以制定如下规则:

  

匹配<span>之间的所有内容但允许级联   像<span>adsa<span>asdsa</span></span>

这样的匹配项

是否有任何regexp-guru知道如何实现这一目标?

1 个答案:

答案 0 :(得分:0)

你可以尝试这样的事情(没有循环):

var $div = $('#the_id_of_ the_div'),
    array = ['word1','word2', 'word1word3'],
    re = new RegExp(array.join('|'), 'gi'),
    divHTML = $div.text().replace(re, "<span class='markedString colorword1orword2'>$&</span>");
$div.html(divHTML);

这只是一个例子,您可能会从帖子中的代码段之外的某些jQuery对象获得div


修改

如果你在包装器中有一堆div,你可以这样做:

var array = ['word1','word2', 'word1word3'],
    re = new RegExp(array.join('|'), 'gi');
$('#wrapper div').each(function () {
    var divHTML = $(this).text().replace(re, "<span class='markedString colorword1orword2'>$&</span>");
    $(this).html(divHTML);
    return;
});

A live demo at jsFiddle