javascript - Compare 2 strings and change color -
i not sure how should go this. 2 string database comma seperated below
string1 = "dd,cc,ff" string2 = "dd,xx,ff"
i bind string1 html table.
you can see 2 strings different. want find string2 in string 1 , highlight changed part of string.
so output string1 = dd,cc,ff
so table show whole string highlight value "cc" in table.
how should go this? open use jquery or javascript.
compare each comma seperated value, if not same, wrap tag of kind :
function checkstrings(str1, str2) { str1 = array.isarray(str1) ? str1 : str1.split(','); str2 = array.isarray(str2) ? str2 : str2.split(','); (var i=0; i<str1.length; i++) { if (str1[i] !== str2[i]) str1[i] = '<b>' + str1[i] + '</b>'; } return str1.join(''); }
or if order doesn't matter :
function checkstrings(str1, str2) { str1 = array.isarray(str1) ? str1 : str1.split(','); (var i=0; i<str1.length; i++) { if (str2.indexof(str1[i]) == -1) { str1[i] = '<b>' + str1[i] + '</b>'; } } return str1.join(''); }
Comments
Post a Comment