5

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.

10
  • 1
    Look for these \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. Commented Sep 26, 2017 at 21:10
  • 1
    .replace() is the javascript function that will replace specific stings of text from other strings of text. w3schools.com/jsref/jsref_replace.asp Commented Sep 26, 2017 at 21:12
  • 1
    @Stephan This is not a duplicate of that question. I tried the code of the choosen answer. It did not work. Commented Sep 26, 2017 at 21:14
  • 1
    Have you tried adjusting the code which sets the character at HTML? Commented Sep 26, 2017 at 21:17
  • 2
    @Stephan, I stand corrected. Didn't notice the button at the bottom. Looks like he is indeed looking for that representation of a new line. Commented Sep 26, 2017 at 21:31

3 Answers 3

5

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>

Sign up to request clarification or add additional context in comments.

1 Comment

This one worked for me
3

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.

Comments

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.