基于 JavaScript 的 diff 实用程序

我正在寻找一个用 JavaScript 编写的 差异等价物,它只返回/打印相关的行。我不希望两个全文显示在彼此之间的差异突出显示,但只想要实际的差异(加上一些缓冲行,以了解差异在哪里) ,类似于从 linux diff 实用程序的输出。

有人知道这样的 javascript 函数吗? 所有的差异都应该被识别(甚至改变空格)。谢谢。

75426 次浏览

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.

jsdifflib looks promising - try the demo linked from that page.

jsdifflib inline mode comparison, try tweaking the context size to display just the desired window of change you want. There's a demo here

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/

There's also google-diff-match-patch from Google

Also available on NPM

npm install diff-match-patch

Check out CodeMirror. Nuff said.

Checkout my minimal implementation: https://github.com/Slava/diff.js

  • 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.

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.

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));