如何使用JavaScript从字符串中删除¶

5
我正在使用一种不同的API工具来创建一个漂亮的差异展示已更改的文本。我使用Google差异工具来完成这个任务。当差异文本生成时,它在每行末尾产生一个字符。我想要删除所有此字符的实例。如何操作呢?这里有一个工具演示

1
@Stephan 这不是那个问题的重复。我尝试了被选答案的代码,但它没有起作用。 - Luke101
1
你尝试过调整设置HTML字符的代码吗? - guest271314
1
这肯定是那个问题的重复。你正在尝试用空字符串替换所有的“¶”实例。这不是一个重复吗? - user47589
1
你的代码长什么样子?在我的例子中它是有效的。 - Stephan
2
@Stephan,我承认错误。没有注意到底部的按钮。看起来他确实在寻找换行符的表示。 - blaze_125
显示剩余5条评论
3个回答

4

如果您不确定API的确切调用方式,这可能会让人感到棘手。但是,您指向的演示中使用的是这些API调用,因此我假设您的返回值类似。替换函数仍然是您想要的,只需要更改要搜索的内容。在本例中,使用 ¶ 代替

const string1 = `I am the very model of a modern Major-General,
I've information vegetable, animal, and mineral,
I know the kings of England, and I quote the fights historical,
From Marathon to Waterloo, in order categorical.`;

const string2 = `I am the very model of a cartoon individual,
My animation's comical, unusual, and whimsical,
I'm quite adept at funny gags, comedic theory I have read,
From wicked puns and stupid jokes to anvils that drop on your head.`;

const dmp = new diff_match_patch;
const diff = dmp.diff_main(string1, string2);

dmp.diff_cleanupSemantic(diff);

const prettyDiff = dmp.diff_prettyHtml(diff)

console.log('Original:', prettyDiff);
console.log('Replaced:', prettyDiff.replace(/¶/g, ''));
<script src="https://neil.fraser.name/software/diff_match_patch/svn/trunk/javascript/diff_match_patch.js"></script>


这个对我起作用了。 - Luke101

3
以下内容可以完成任务:

var str = 'abc¶def';
var replaced = str.replace(/¶/g, '');
console.log(str);
console.log(replaced);

然而需要注意的是,Diff库本身甚至不会返回段落标记:

var dmp = new diff_match_patch();
var diff = dmp.diff_main(inp1, inp2);
// maybe also call dmp.diff_cleanupSemantic(diff);

使用这个代码片段,您可以轻松地获取inp1inp2之间的变更数组。


1

 var b = "¶this¶Is¶¶¶¶Just¶a¶RandomString¶";
 // b.replace(/\u00B6/g,''); or 
 // b.replace(/¶/g,'')
console.log(b);
console.log(b.replace(/\u00B6/g,'')); //  ==> using the unicode of character
console.log(b.replace(/¶/g,'') )


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接