[英]How to replace text not within a specific-Tag in JavaScript
I have a string (partly HTML) where I want to replace the string :-)
into bbcode :wink:
.我有一个字符串(部分是 HTML),我想将字符串
:-)
替换为 bbcode :wink:
。 But this replacement should not happen within <pre>
, but in any other tag (or even not within a tag).但是这种替换不应该发生在
<pre>
中,而应该发生在任何其他标签中(甚至不在标签中)。
For example, I want to replace例如,我想替换
:-)<pre>:-)</pre><blockquote>:-)</blockquote>
to:至:
:wink:<pre>:-)</pre><blockquote>:wink:</blockquote>
I already tried it with the following RegEx, but it does not work (nothing gets replaced):我已经使用以下 RegEx 进行了尝试,但它不起作用(没有任何内容被替换):
var s = ':-)<pre>:-)</pre><blockquote>:-)</blockquote>';
var regex = /:\-\)(?!(^<pre>).*<\/pre>)/g;
var r = s.replace(regex, ':wink:');
Can someone please help me?有人可以帮帮我吗? :-)
:-)
You could avoid hellish regexes altogether if you use a suitable library such as jQuery, eg: 如果使用合适的库(例如jQuery),则可以完全避免使用令人讨厌的正则表达式,例如:
var excludeThese = ['pre'];
// loop over all elements on page, replacing :-) with :wink: for anything
// that is *not* a tag name in the excludeThese array
$('* not:(' + excludeThese.join(',') + ')').each(function() {
$(this).html($(this).html().replace(/:\-\)/,':wink:'));
});
This ought to do it:- 这应该做到:
var src = ":-)<pre>:-)</pre><blockquote>:-)</blockquote>"
var result = src.replace(/(<pre>(?:[^<](?!\/pre))*<\/pre>)|(\:\-\))/gi, fnCallback)
function fnCallback(s)
{
if (s == ":-)") return ":wink:"
return s;
}
alert(result);
It works because any pre
element will get picked up by the first option in the regex and once consumed means that any contained :-) can't be matched since the processor will have moved beyond it. 之所以起作用,是因为任何
pre
元素都将由正则表达式中的第一个选项拾取,并且一旦消耗就意味着任何包含的:-)都无法匹配,因为处理器将移出它。
Just thought it'd be worth offering a DOM solution: 只是认为值得提供DOM解决方案:
Eg 例如
var div = document.createElement('div');
div.innerHTML = ":-)<pre>:-)</pre><blockquote>:-)</blockquote>";
replace(div, /:-\)/g, ":wink:", function(){
// Custom filter function.
// Returns false for <pre> elements.
return this.nodeName.toLowerCase() !== 'pre';
});
div.innerHTML; // <== here's your new string!
And here's the replace
function: 这是
replace
功能:
function replace(element, regex, replacement, filter) {
var cur = element.firstChild;
if (cur) do {
if ( !filter || filter.call(cur) ) {
if ( cur.nodeType == 1 ) {
replace( cur, regex, replacement );
} else {
cur.data = cur.data.replace( regex, replacement );
}
}
} while ( cur = cur.nextSibling );
}
尝试使用var regex = /:-)(?!(^)* </ pre>)/ g;
Almost good: Your negative lookbehind and lookahead where not in the right position and need a slight adjustment:几乎很好:您的负面回顾和展望不在正确的 position 中,需要稍作调整:
/(?<!(<pre>)):-\)(?!(<\/pre>))/g
as a side effect though: <pre>:-):-)</pre>
works too but not <pre>:-):-):-)</pre>
作为副作用:
<pre>:-):-)</pre>
也可以,但不是<pre>:-):-):-)</pre>
https://regex101.com/r/CO0DAD/1 https://regex101.com/r/CO0DAD/1
ps.附言。 this is a firefox 104 browser (could be different in others)
这是一个 firefox 104 浏览器(其他浏览器可能不同)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.