title: JS Regex: (a|b) Matches the a or the b part of the subexpression. Different behaviour when a and b switched
body: <p>I am trying to replace <em>2%</em> with ''(empty) using regexp concept. If input string is either <em>%</em> or <em>2%</em>, it should be replaced with ''(empty):</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>const str = "2%";
  
  console.log(`2%`.replace(/^\d%$|\d(?=%)/, ''));
  console.log(`2%`.replace(/\d(?=%)|^\d%$/, ''));</code></pre>
  </div>
  </div>
  </p>
  
  <p><code>(a|b) Matches the a or the b part of the subexpression.</code> </p>
  
  <p><code>"2%".replace(/^\d%$|\d(?=%)/, '')</code>. This works well.</p>
  
  <p>But, <code>"2%".replace(/\d(?=%)|^\d%$/, '')</code> does not.</p>