これが要件に適合するかどうかはわかりませんが、変数は私にはHTML文字列のように見えます。元のメイン要素を失うことなく、文字列を単語ごとに比較したいようです。次のようにします。
var obj1 = "<p>the quick <b>brown</b> fox jumped over the fence</p>",
obj2 = "<p>the quick <b>brown</b> fox </p>";
var obj3 = compare(obj1, obj2);
$('body').append(obj3);
function compare(elm1, elm2) {
var obj = [],
o1 = String($(elm1).html()).split(' '), //get <p>'s inner HTML as string and split on spaces to get each word
o2 = String($(elm2).html()).split(' ');
$.each(o1, function(i,e) { //iterate over each word inside <p> of first string
if (e !== o2[i]) obj.push(e); //check if matches each word in the same loaction in the second string
});
//we should now have an array of the words that does not match each other
return $(elm1).clone().html(obj.join(' ')); //join with new spaces and return the string as html contained in a clone of the original element.
}
フィドル
呼び出す関数としてのFIDDLE (より単純なようです)!
これは次のようなものでは機能しないことを追加する必要があります:
var obj1 = "<p>the quick <b>brown</b> fox jumped over the fence</p>",
obj2 = "<p>fox jumped over the fence</p>";
これらの文字列は単語の一致を表すものではないため、2番目は最初の文字列などの一部ですが、このようなイベントで文字列の同様の部分を削除するには、いつでも次のようにすることができます。
var obj1 = "<p>the quick <b>brown</b> fox jumped over the fence</p>",
obj2 = "<p>fox jumped over the</p>",
o1 = String($(obj1).html()),
o2 = String($(obj2).html());
var obj = $(obj1).clone().html(o1.replace(o2, ''));
フィドル