-3

我正在尝试制作一个 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
4

1 回答 1

1

您可以使用replace正则表达式:

一般情况(这里提琴):

function escapeRegExp(str) { return str.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, "\\$&"); }
var A = 'AAA'; // string A
var B = 'BBB'; // string B
var testStr = "BEFORE AAA WILL BE REMOVED BBB AFTER CCC BBB again DDD"; // string to be replaced

var stopAtFirstBMatch = testStr.replace(new RegExp(escapeRegExp(A)+'(.*?)'+escapeRegExp(B)), '');
console.assert(stopAtFirstBMatch == "BEFORE  AFTER CCC BBB again DDD");

var stopAtLastBMatch = testStr.replace(new RegExp(escapeRegExp(A)+'(.*)'+escapeRegExp(B)), '');
console.assert(stopAtLastBMatch == "BEFORE  again DDD");

你的情况(在这里小提琴):

正则表达式Name(.*?)end of post将匹配从它找到'Name'的第一个开始的任何内容。'end of post'(.*?)是非贪婪匹配一切);

如果您的意思end of post是帖子的实际结束,您可以Name(.*)用作正则表达式。它将匹配以 . 开头的每个文本Name

var strTest = "Hello my Name is this thal go away until the end of post problem goes away."

var regexEndOfPost = "Name(.*?)end of post"
// alerts: 'Hello my  problem goes away'
alert(strTest.replace(new RegExp(regexEndOfPost), ''));

var regexNameToEnd = "Name(.*)" // in case you want from 'Name' to the end of the string
console.log(strTest.replace(new RegExp(regexNameToEnd), ''));
// alerts: 'Hello my '
alert(strTest.replace(new RegExp(regexNameToEnd), ''));
于 2013-05-11T18:27:20.837 回答