Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove ¶ from a string using javascript [duplicate]

Tags:

javascript

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.

like image 339
Luke101 Avatar asked Sep 26 '17 21:09

Luke101


2 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>
like image 144
Ben Avatar answered Sep 21 '22 18:09

Ben


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.

like image 23
Stephan Avatar answered Sep 18 '22 18:09

Stephan