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

用于匹配不属于HTML标记的文本的Javascript RegExp,javascript,html,regex,replace,lookbehind,Javascript,Html,Regex,Replace,Lookbehind,我试图找到一种方法来突出显示HTML中的一些文本。给出了以下HTML: <div>This text contains matching words like word1 and word2 and xyzword1xyz and word2xyz and xyzword2</div> 我当前的Javascript: $.each(array , function(index, elem){ if(elem.length<3 || elem

我试图找到一种方法来突出显示HTML中的一些文本。给出了以下HTML:

<div>This text contains matching words like word1 and word2 and xyzword1xyz and word2xyz and xyzword2</div>
我当前的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>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的方法,以便制定如下规则:

匹配不在两者之间但允许级联的所有内容 像ADSA这样的匹配


是否有任何regexp专家知道如何对此进行归档?

您可以尝试以下类似的方法:无循环:

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);

.

要替换的单词只包含字母和数字,而不包含RegExp中使用的任何特殊字符?div真的存在于页面上吗?是的,div与页面上的div完全相同。要替换的单词是字母、数字和-和u。太好了!非常感谢-这完全解决了我的问题!!我必须将您的代码转换为var re=new RegExparray.join'|','gi';returnString=returnString.replace,functionmatch,contents,offset,s{return+match+;};现在它非常适合我!这是一个多么简单却又如此直截了当的想法,一步完成更换!哦,我明白了,我部分误解了你的问题。我认为类似于要替换的单词的类名只是为了让事情变得更难。。。
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>
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);
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;
});