title: R string matching and replace
body: <p>Starting with the string <code>-theta[0] * x[0] * x[4] -theta[1] * x[1]</code>, I'd like capture <code>theta[.]</code> and replace with <code>exp(theta[.])</code> giving the final result as:</p>
  
  <p><code>-exp(theta[0]) * x[0] * x[4] -exp(theta[1]) * x[1]</code></p>
  
  <p>How do I accomplish this in R?</p>
  
  <p>I tried the following to test by matching between <code>a</code>s as follows:</p>
  
  <pre><code>p &lt;- c("abba", "abcdab")                                                      
  gsub("\\(a[^a]*a\\)", "|\\1|", p)
  </code></pre>
  
  <p>I expected the output to be something like </p>
  
  <pre><code>|abba|
  |abcda|b
  </code></pre>
  
  <p>But the output is</p>
  
  <pre><code>[1] "abba"   "abcdab"
  </code></pre>
  
  <p>What is the proper regex expression to achieve the desired result?</p>
  
  <p>Thanks in advance.</p>