问题
I'm looking for a diff equivalent written in JavaScript that only returns/prints relevant lines. I don't want both full text displayed next to each other with the differences highlighted, but just want the actual differences (plus some buffer lines to know where the difference is), similar to the output from the linux diff utility.
Does anybody know a javascript function that does this? All differences should be recognized (even changed whitespace). Thanks.
回答1:
jsdifflib inline mode comparison, try tweaking the context size to display just the desired window of change you want. There's a demo here
回答2:
I completely rebuilt the jsdifflib utility for speed. Since my version does not require DOM access it at least 4.5 times faster, and it is also extended to highlight character differences in each line.
http://prettydiff.com/diffview.js
You can test this out with the online tool directly at http://prettydiff.com/
回答3:
There's also google-diff-match-patch from Google
Also available on NPM
npm install diff-match-patch
回答4:
- Mergely is totally worth checking out. CodeMirror-based, client-side only.
- Then there's the CodeMirror demo which requires server-side diff computation.
- jsdifflib and prettydiff as mentioned in other answers.
回答5:
Checkout my minimal implementation: https://github.com/Slava/diff.js
回答6:
I dont know much about the diff utility in linux or linux in general, but this might be what you are looking for jsdifflib. You can find a live example there and see if it works for you.
回答7:
Check out CodeMirror. Nuff said.
回答8:
Check out the the wikEd diff JavaScript library. There is also an online tool.
wikEd diff features inline text comparisons with block move highlighting and character/word-based resolution. It is optimized for Wikipedia source text, but works great for any type of text or code. The library code is fully customizable, has Unicode support, is extensively commented, and is in the public domain.
回答9:
old question i know, but if your doing node work or want something that is compatible with requirejs/commonjs module
I've really liked https://www.npmjs.com/package/diff
console.log(diff.createPatch('some file name.txt', expected, actual));
回答10:
jsdifflib looks promising - try the demo linked from that page.
来源:https://stackoverflow.com/questions/3053587/javascript-based-diff-utility