我正在尝试制作一个 chrome 扩展,以通过沉默替换论坛中已知巨魔的评论。
有没有办法告诉 .replace 将文本从起点替换到终点?即:用“”替换以“名称”开头直到“帖子结束”的文本
提前致谢
编辑:满足评论:
walk(document.body);
function walk(node)
{
var child, next;
switch ( node.nodeType )
{
case 1: // Element
case 9: // Document
case 11: // Document fragment
child = node.firstChild;
while ( child )
{
next = child.nextSibling;
walk(child);
child = next;
}
break;
case 3: // Text node
handleText(node);
break;
}
}
function handleText(textNode)
{
var v = textNode.nodeValue;
var regexReplace = "Name of troll(.*)"
v = v.replace(new RegExp(regexReplace), '');
textNode.nodeValue = v;
}
这是 html 的样子:
class="up" style="padding-left: 14px" id="pid_31519766" data-pid="31519766"><div class="posting08 ch_wissenschaft"><div class="thread" style="width: 505px" id="t31519766" data-pid="31519766"><div class="row1"><div class="l" style="width:336px "><a class="offset" name="pid31519766"></a><div class="uname" style="max-width:277px "><a rel="nofollow" href="/Userprofil/ByPosting/31519766">NAME OF TROLL</a></div><div class="utools"><a rel="nofollow" class="follow" onclick="STD.FollowingRelationships.followUser(31519766,1363710557719,escape('Orakel von Silesia'))" title="Mitposter von „NAME OF TROLL” werden. „NAME OF TROLL” hat 7 Mitposter.">7</a></div></div><div class="r"><a class="std-button rate p" title="Sehr lesenswert" rel="nofollow" onclick="STD.Forum.rate('31519766',1);">+</a></div><div class="r"><a class="std-button rate n" title="Nicht lesenswert" rel="nofollow" onclick="STD.Forum.rate('31519766', 0);">-</a></div><div class="r rating"><span class="l counter n">14</span><span class="l bar"><span class="wrapper"><span class="n"></span><span class="p"></span></span></span><span class="l counter p">1</span></div><div class="clear"></div></div><div class="row2"><div class="l date" data-timestamp="11.5.2013 15:08:13"><span class="absolute">11.5.2013, 15:08</span><span class="relative"></span></div><div class="r a"><a rel="nofollow" title="Klicken Sie hier, wenn Sie auf dieses Posting antworten möchten." class="onclick" onclick="STD.Forum.post({action: 'reply', id: '31519766', trigger: this}); return false;">antworten</a></div><div class="r p"><a rel="nofollow" title="Mit diesem Link können Sie die URL des Postings aufrufen." href="/plink/1363710557719/31519766">permalink</a></div><div class="r m"><a rel="nofollow" title="Melden Sie Postings an die Redaktion, wenn diese den Community Richtlinien widersprechen." onclick="STD.Forum.report('31519766');">melden</a></div><div class="clear"></div></div></div><div
class="txt" style="width: 505px">
POST I WANNA GET RID OF
</p></div></div></div><div