Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // str1 is the reference text.
- var str1 = "I was sent to earth to protect my cousin";
- // str2 is the text which I want to compare with str1.
- var str2 = "I was send to earth to protect my sister"
- function words(s) {
- return s.toLowerCase().match(/w+/g);
- }
- let a = words(str1);
- let b = words(str2);
- let res1 = b.filter(i => !a.includes(i));
- let res2 = a.filter(i => !b.includes(i));
- highlight(b, "str2", res1);
- function highlight(str, id, res){
- var text = "";
- for(var i=0; i<str.length; i++){
- var hasVal = res.includes(str[i]);
- if(hasVal){
- text +=" <span style='color:red'>"+str[i]+"</span> ";
- } else {
- text +=" "+str[i]+" ";
- }
- }
- document.write(text);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement