SHARE
TWEET

Untitled

a guest Jun 16th, 2019 50 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // str1 is the reference text.
  2.     var str1 = "I was sent to earth to protect my cousin";
  3.  
  4.     // str2 is the text which I want to compare with str1.
  5.     var str2 = "I was send to earth to protect my sister"
  6.  
  7.     function words(s) {
  8.         return s.toLowerCase().match(/w+/g);
  9.     }
  10.  
  11.     let a = words(str1);
  12.     let b = words(str2);
  13.     let res1 = b.filter(i => !a.includes(i));
  14.     let res2 = a.filter(i => !b.includes(i));
  15.  
  16.     highlight(b, "str2", res1);
  17.     function highlight(str, id, res){
  18.         var text = "";
  19.        for(var i=0; i<str.length; i++){
  20.         var hasVal = res.includes(str[i]);
  21.         if(hasVal){
  22.           text +=" <span style='color:red'>"+str[i]+"</span> ";
  23.         } else {
  24.            text +=" "+str[i]+" ";
  25.         }
  26.        }
  27.         document.write(text);
  28.     }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top