Javascript compare strings and get end difference

I have two strings. String A: "The quick brown fox" String B: "The quick brown fox jumps over the lazy dog."

String B will always contain string A verbatim. There will never be a "quick black fox" or a "quick and speedy brown fox".

How do a get a "String C" of the difference "jumps over the lazy dog."?



const A = "The quick brown fox"

const B = "The quick brown fox jumps over the lazy dog."

const diff = (diffMe, diffBy) => diffMe.split(diffBy).join('')

const C = diff(B, A)

console.log(C) // jumps over the lazy dog.


See the basic example below. This can easily be modified/extended for different behaviour.

var stringA = document.getElementById('string_a').textContent,
    stringB = document.getElementById('string_b').textContent,
    firstOccurrence = stringB.indexOf(stringA);

if(firstOccurrence === -1)
  alert('Search string Not found');
  var stringALength = stringA.length;
  var newString;

  if(firstOccurrence === 0)
    newString = stringB.substring(stringALength);
    newString = stringB.substring(0, firstOccurrence);
    newString += stringB.substring(firstOccurrence + stringALength);

  document.getElementById('diff').textContent = newString;
<p>String A: <span id="string_a">The quick brown fox</span></p>
<p>String B: <span id="string_b">The quick brown fox jumps over the lazy dog</span></p>
<p>Difference: <span id="diff"></span></p>


comu's answer as a function...

function compareString( s1, s2, splitChar ){
    if ( typeof splitChar == "undefined" ){
        splitChar = " ";
    var string1 = new Array();
    var string2 = new Array();

    string1 = s1.split( splitChar );
    string2 = s2.split( splitChar );
    var diff = new Array();

        var long = string1;
    else {
        var long = string2;

    return diff;    
compareString( "?Yo=dude", "?Yo=Dude&do=roby", "&" ).join('\n');
compareString( "?Yo=Dude", "?Yo=Dude&do=roby", "&" ).join('\n');

Note: this answer solves the issue of finding extra query parameters (based on another query string), and is not an exact answer for the OP.


You need to cross check each word to the other one.

var s1 = "The quick brown fox",
  s2 = "The quick brown fox jumped over the fence",
  string1 = new Array(),
  string2 = new Array(),
  diff = new Array(),

string1 = s1.split(" ");
string2 = s2.split(" ");

if (s1.length > s2.length) {
  longString = string1;
} else {
  longString = string2;

for (x = 0; x < longString.length; x++) {
  if (string1[x] != string2[x]) {

document.write("The difference in the strings is " + diff.join(" "));


Check out this site for a wonderful API to see the difference between strings: google-diff-match-patch You might need to check the UI according to your need though.


If "String B will always contain string A verbatim", won't the following suffice?

var c = b.replace(a, "");

I found the simple way.

Here is the code:

var t1 = "The quick brown fox";
var t2 = "The quick brown fox jumps over the lazy dog.";
var indxFoundStart =; //first index of the position of t1 found in t2 (not use but you can print for sure)
var indxFoundEnd = t1.length; //last index of t1 found in t2
var textCompareLen = t2.length; // length of the longer string

// You can add the condition to verify which text is shorter, which text is longer if you do not want to fixed in t1 and t2
document.write("difference string is: " + t2.substring(indxFoundEnd, textCompareLen));

I have try with 2 sample, the text with space and the text without space and it work fine.

Absolutely, I have try with your example, and it work fine too.


Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us

©2020 All rights reserved.