I am using a diff api tool to create a nice diff to show changed text. I am using the google diff tool to accomplish this. When the diff text is generated it produces a ¶ at the end of each line. I want to remove all instances of this character. How would I go about doing it? Here is a demo of the tool.
3 Answers
Not knowing exactly what you're calling on the API is tricky, but these are the API calls used on the demo you linked, so I'm assuming your return is something similar. The replace function is still what you want, you just need to change what you're searching for. In this case ¶ instead of ¶
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>
1 Comment
Luke101
This one worked for me
The following should do the job:
var str = 'abc¶def';
var replaced = str.replace(/¶/g, '');
console.log(str);
console.log(replaced);
However be aware, that the Diff library itself doesn't even return the paragraph marks:
var dmp = new diff_match_patch();
var diff = dmp.diff_main(inp1, inp2);
// maybe also call dmp.diff_cleanupSemantic(diff);
With that snippet you simply receive an array of changes between inp1 and inp2.
\r\n\r\n. Depending what system your data is coming from, you may have to look for all 3, or maybe just 1 of them..replace()is the javascript function that will replace specific stings of text from other strings of text. w3schools.com/jsref/jsref_replace.asp