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.
Asked
Active
Viewed 886 times
5

Luke101
- 63,072
- 85
- 231
- 359
-
1Look 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. – blaze_125 Sep 26 '17 at 21:10
-
1`.replace()` is the javascript function that will replace specific stings of text from other strings of text. https://www.w3schools.com/jsref/jsref_replace.asp – WizardCoder Sep 26 '17 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. – Luke101 Sep 26 '17 at 21:14
-
1Have you tried adjusting the code which sets the character at HTML? – guest271314 Sep 26 '17 at 21:17
-
1This certainly looks like a duplicate of that question. You're trying to replace all instances of "¶" with an empty string. How is that not a duplicate? – Sep 26 '17 at 21:19
-
1How did your code look like? In my example it works. – Stephan Sep 26 '17 at 21:20
-
1@Stephan, the problem is that `¶` is not the actual character he is after. That's just the pictograph that represents a new line or a carriage return. Under the hood... that `¶` could be a `\r`, `\n` or `\r\n`. – blaze_125 Sep 26 '17 at 21:25
-
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. – blaze_125 Sep 26 '17 at 21:31
-
1@Luke101 which calls specifically are you using from the API? – Ben Sep 26 '17 at 21:33
-
1@Luke101 are you sure that you are using the API as intended? The `diff_main` function returns an array of changes, there should be no need to remove the paragraph mark. – Stephan Sep 26 '17 at 21:34
3 Answers
4
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>

Ben
- 5,079
- 2
- 20
- 26
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
.

Stephan
- 2,028
- 16
- 19
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,'') )

Satish Kumar
- 601
- 6
- 14