2017-09-26 65 views
0

我正在使用diff api工具創建一個很好的差異來顯示更改後的文本。我正在使用google diff tool來完成此操作。當生成差異文本時,它會在每行的末尾生成一個。我想刪除這個角色的所有實例。我該怎麼去做呢?這是該工具的一個demo如何使用javascript從字符串中刪除¶

+0

尋找這些'\ r''\'N' \ r \ N'。根據您的數據來自何種系統,您可能需要查找全部3個,或者只是其中1個。 –

+0

'.replace()'是將從其他字符串替換文本的特定字符串的JavaScript函數。 https://www.w3schools.com/jsref/jsref_replace.asp – WizardCoder

+0

@Stephan這不是這個問題的重複。我嘗試了選擇的答案的代碼。它不起作用。 – Luke101

回答

1

不知道你叫什麼的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>

+0

這一個爲我工作 – Luke101

1

下應該做的工作:

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之間的一系列更改。

0

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,''))

相關問題