< div id = " txt1 " style = " border:1px solid black " > Hello123 World 123 </ div > < div id = " txt2 " style = " border:1px solid black " > Hello World 456 </ div > < div id = " txt3 " style = " border:1px solid red " ></ div > < div id = " txt4 " style = " border:1px solid red " ></ div > < script type = " text/javascript " > <!-- function compare_str(str1,str2) ... { var res1 = ""; var res2 = ""; while(str1.length && str2.length)...{ var arr = find_pos(str1,str2); if (arr && arr.length) ...{ if (arr[0]) ...{ res2 +=("<font color='green'>" + str2.substr(0,arr[0]) + "</font>"); str2 = str2.substr(arr[0]); } res1 +=(str1.substr(0,arr[1])); res2 +=(str2.substr(0,arr[1])); str1 = str1.substr(arr[1]); str2 = str2.substr(arr[1]);