我还使用了m.buettner推荐的反向方法,根据您的模式,它可能会变得非常棘手。如果您匹配简单的模式或字符串,我发现这种解决方法效果很好。
话虽如此,我想我会为了好玩而跳出框框。该解决方案并非没有自己的弱点,但它也可以工作,并且应该很容易适应具有中到复杂正则表达式的现有代码。
http://jsfiddle.net/52QBx/
js:
function negativeLookBehind(lookBehindRegExp, matchRegExp, modifiers)
{
var text = $('#content').html();
var badGoodRegex = regexMerge(lookBehindRegExp, matchRegExp, modifiers);
var badGoodMatches = text.match(badGoodRegex);
var placeHolderMap = {};
for(var i = 0;i<badGoodMatches.length;i++)
{
var match = badGoodMatches[i];
var placeHolder = "${item"+i+"}"
placeHolderMap[placeHolder] = match;
$('#content').html($('#content').html().replace(match, placeHolder));
}
var text = $('#content').html();
var goodRegex = matchRegExp;
var goodMatches = text.match(goodRegex);
for(prop in placeHolderMap)
{
$('#content').html($('#content').html().replace(prop, placeHolderMap[prop]));
}
return goodMatches;
}
function regexMerge(regex1, regex2, modifiers)
{
/*this whole concept could be its own beast, so I just asked to have modifiers for the combined expression passed in rather than determined from the two regexes passed in.*/
return new RegExp(regex1.source + regex2.source, modifiers);
}
var result = negativeLookBehind(/(bad )/gi, /(good\d)/gi, "gi");
alert(result);
html:
<div id="content">Some random text trying to find good1 text but only when that good2 text is not preceded by bad text so bad good3 should not be found bad good4 is a bad oxymoron anyway.</div>
主要思想是找到所有总模式(包括后向匹配和真正匹配),并暂时从正在搜索的文本中删除这些模式。我使用了一张地图,因为隐藏的值可能会有所不同,因此每个替换都必须是可逆的。然后,我们可以只为您真正想要找到的项目运行正则表达式,而不会妨碍那些与后视相匹配的项目。确定结果后,我们换回原始项目并返回结果。这是一个古怪但实用的解决方法。