我正在使用一种不同的API工具来创建一个漂亮的差异展示已更改的文本。我使用Google差异工具来完成这个任务。当差异文本生成时,它在每行末尾产生一个
¶
字符。我想要删除所有此字符的实例。如何操作呢?这里有一个工具演示。¶
字符。我想要删除所有此字符的实例。如何操作呢?这里有一个工具演示。如果您不确定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>
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);
使用这个代码片段,您可以轻松地获取inp1
和inp2
之间的变更数组。
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,'') )