Javascript Diff Algorithm

Tagged:  

Another tool for your javascript toolbelt!

John Resig implemented a diff algorithm in javascript that includes two functions, one of which is recommended for use:

diffString( String oldFile, String newFile ) This method takes two strings and calculates the differences in each. The final result is the 'newFile' marked up with HTML (to signify both deletions from the oldFile and additions to the newFile).

Sample Code

document.body.innerHTML = diffString(
"The red brown fox jumped over the rolling log.",
"The brown spotted fox leaped over the rolling log"
);

Sample Output

The red brown spotted fox jumped leaped over the rolling log.

Click here to visit the site and download the code!

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <pre> <div> <blockquote> <object> <embed> <img> <param>
  • Lines and paragraphs break automatically.
  • Web page addresses and e-mail addresses turn into links automatically.

More information about formatting options

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
Copy the characters (respecting upper/lower case) from the image.