eg. : font<font color='red'>font</font>
假如我要替換f或o或n或t或等等 為 <b>f或o或n或t或等等</b>
但是又不希望<font和</font> 標籤中的font中替換掉。比如我要替換的是on
則我需要的結果是
f<b>on</b>t<font color='red'>f<b>on</b>t</font>我像了半天也沒想到什麼辦法。
故來求教
假如我要替換f或o或n或t或等等 為 <b>f或o或n或t或等等</b>
但是又不希望<font和</font> 標籤中的font中替換掉。比如我要替換的是on
則我需要的結果是
f<b>on</b>t<font color='red'>f<b>on</b>t</font>我像了半天也沒想到什麼辦法。
故來求教
var str ="<t>font<font color='red'>font</font>aonf"str = str.replace(/([^<>]*)<|>([^<>]*)<|>([^<>]*)/g,
function($_1){
return $_1.replace('on','<b>on</b>')
})
alert(str)
"font<font color='red'>font</font>".replace(/(?=[^>]*(?=<))[font]/gi, "<b>$&</b>")
2、“比如我要替換的是on”:
"font<font color='red'>font</font>".replace(/(?=[^>]*(?=<))on/gi, "<b>$&</b>")
3、哈哈,下次记得多给点分。
。
不过需要微调
"font<font color='red'>font</font>aonf".replace(/(?=[^>]*(?=<|$))on/gi, "<b>$&</b>")
然后如果用零宽,php较新的版本可以这样。$str = "font<font color='red'>font</font>aonf";
echo preg_replace('#(?<!<f|</f)on#i', '<b>$0</b>',$str);