title: Postgres regexp replace using another table for both the regexp and replacement values
body: <p>Let's say I have a table with key/values where the key is a regular expression and the corresponding value is the replacement value. For example:</p>
  
  <pre><code>table_a:
  
  Key   Value
  -----------
  a     123
  b     456
  c     789
  </code></pre>
  
  <p>I need to update a value in another table using SQL by replacing every key that appears in the table above with it's corresponding value.</p>
  
  <p>If I had a single replacement value, I would use something like this:</p>
  
  <pre><code>UPDATE table_b 
  SET    some_field = REGEXP_REPLACE((SELECT STRING_AGG(table_a.key, '|') 
                                      FROM   table_a), 'replacement value'); 
  </code></pre>
  
  <p>This would construct a single regular expression based on all the <code>keys</code> in table_a and replace any occurrence with my replacement string.</p>
  
  <p>How could I do something similar but use the corresponding <code>value</code> from table_a as the replacement value?</p>
  
  <p>Using Postgres 9.5</p>