目前要在JavaScript中搜索和替换字符串,我使用下面的代码:
var regEX = new RegExp(toFind,"gi");
var newString = oldString.replace(regEx,toReplace);
其中OldString包含大量HTML标记。
我需要做的是在执行替换功能期间我不要希望在<b></b>
标记之间的文本上进行搜索和替换。
我怎样才能在Js中这样做?还有其他方法可以替换吗?
作为示例,请考虑以下字符串:
<div id="testDiv">
<h2>In on announcing if of comparison pianoforte projection</h2>
<p>Compliment interested discretion estimating on stimulated apartments oh. Dear so sing when in find read of call. As distrusts behaviour abilities defective is. Never at water me might. On formed merits hunted unable merely by mr whence or. Possession the unpleasing simplicity her uncommonly.</p>
<b>Kept in sent gave feel will oh it we. Has pleasure procured men laughing shutters nay. Old insipidity motionless continuing law shy partiality. Depending acuteness dependent eat use dejection. Unpleasing astonished discovered not nor shy. Morning hearted now met yet beloved evening. Has and upon his last here must.</b>
<p>New the her nor case that lady paid read. Invitation friendship travelling eat everything the out two. Shy you who scarcely expenses debating hastened resolved. Always polite moment on is warmth spirit it to hearts. Downs those still witty an balls so chief so. Moment an little remain no up lively no. Way brought may off our regular country towards adapted cheered.</p>
<p><b>Not To be Replaced</b>Am no an listening depending up believing. Enough around remove to barton agreed regret in or it. Advantage mr estimable be commanded provision. Year well shot deny shew come now had. Shall downs stand marry taken his for out. Do related mr account brandon an up. Wrong for never ready ham these witty him. Our compass see age uncivil matters weather forbade her minutes. Ready how but truth son new under.</p>
</div>
&#13;
答案 0 :(得分:-2)
我想,这就是你要找的东西:
var re = /[^<b>]*[^<\/b>]/gi;
var str = '<b>Heloo..!,</b>Mr.Its<b>too hot</b>.oops!'
var rstr = str.replace(re,'nooo');
console.log(rstr);// prints <b>nooo</b>nooo<b>nooo</b>nooo