title: Seemingly incorrect regex evaluation in regexp_replace
body: <p>I just stumbled upon a curious behavior of <code>regexp_replace</code> PostgreSQL function. It looks like a bug but I always doubt myself first. When I run</p>
  
  <pre><code>SELECT regexp_replace(E'1%2_3', '([_%])', E'\\ \\1', 'g')
  </code></pre>
  
  <p>it correctly prefixes either underscore or percent with backslash+space and produces "<code>1\ %2\ _3</code>". However when I remove space (it doesn't have to be space, can be any character)</p>
  
  <pre><code>SELECT regexp_replace(E'1%2_3', '([_%])', E'\\\\1', 'g')
  </code></pre>
  
  <p>it stops using captured parenthesized expression in substitution and produces "<code>1\12\13</code>" instead of "<code>1\%2\_3</code>". I would appreciate if someone could tell me what am I doing wrong. I simply need to add backslash before certain characters in a string.</p>
  
  <p><strong>UPDATE</strong>: I was able to achieve the desired behavior by running</p>
  
  <pre><code>SELECT regexp_replace(E'1%2_3', '([_%])', E'\\\\\\1', 'g')
  </code></pre>
  
  <p>My original example still seems a bit illogical and inconsistent. The inconsistency is that using the same <code>E'...'</code> syntax 4 backslashes may produce different result.</p>