Regex experts please help to see if this problem can be solved by regex:
Given string 1 is any string
And string 2 is any string containing all parts of string 1 (but not a simple match -- I will give example)
How to use regex to replace all parts of string 1 in string 2 with blank so that what's remained is the string not in string 1?
For example: str1 = "test xyz"; str2 = "test ab xyz"
I want " ab" or "ab " back. What is the regex I can write so that when I run a replace function on str2, it will return " ab"?
Here is some non-regex code:
function findStringDiff(str1, str2) {
var compareString = function(str1, str2) {
var a1 = str1.split("");
var a2 = str2.split("");
var idx2 = 0;
a1.forEach(function(val) {
if (a2[idx2] === val) {
a2.splice(idx2,1);
} else {
idx2 += 1;
}
});
if (idx2 > 0) {
a2.splice(idx2,a2.length);
}
return a2.join("");
}
if (str1.length < str2.length) {
return compareString(str1, str2);
} else {
return compareString(str2, str1);
}
}
console.log(findStringDiff("test xyz","test ab xyz"));