Get started with diff_match_patch CDN

Apache-2.0 licensed

Library: Diff Match Patch. Efficiently computes, applies text differences between strings.

Tags:
  • diff
  • match
  • patch
  • difference

Stable version

Copied!

How to start using diff_match_patch CDN


// Include the diff_match_patch library from the CDN
const diffMatchPatch = (() => {
  const script = document.createElement('script');
  script.src = 'https://cdn.cdnhub.io/diff_match_patch/20121119/diff_match_patch.js';
  document.head.appendChild(script);

  return new Promise((resolve) => {
    script.onload = () => {
      document.head.removeChild(script);
      resolve(diff_match_patch);
    };
  });
})();

// Two strings to compare
const oldText = "The quick brown fox jumps over the lazy dog.";
const newText = "The quick red fox jumps over the lazy cat.";

// Initialize diff_match_patch and compare the strings
async function compareStrings() {
  const dmp = await diffMatchPatch;
  const diff = dmp.diff_main(oldText, newText, null);
  const patch = dmp.diff_apply(null, oldText, diff);

  // Display the differences
  const output = document.createElement('pre');
  output.innerHTML = dmp.diff_prettyHtml(diff);
  document.body.appendChild(output);
}

compareStrings();
Copied!
Copied!

All versions