title: Finding the difference between two string in Javascript with regex
body: <p>Regex experts please help to see if this problem can be solved by regex:</p>
  
  <p>Given string 1 is any string</p>
  
  <p>And string 2 is any string containing all parts of string 1 (but not a simple match -- I will give example)</p>
  
  <p>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?</p>
  
  <p>For example:
  str1 = "test xyz";
  str2 = "test ab xyz"</p>
  
  <p>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"?</p>
  
  <p>Here is some non-regex code:</p>
  
  <pre><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 &gt; 0) {
                          a2.splice(idx2,a2.length);
                      }
                      return a2.join("");
                  }
  
                  if (str1.length &lt; str2.length) {
                      return compareString(str1, str2);
                  } else {
                      return compareString(str2, str1);
                  }
              }
  
              console.log(findStringDiff("test xyz","test ab xyz"));
  </code></pre>