title: Replace string unless between two points
body: <p>I have the regex need to replace all backslashes <code>\\</code> with <code>\"</code> unless the <code>\\</code> is between two dollar signs <code>$\\bar{x}$</code>.  I don't know how to say in regex replace all these unless it falls between these two characters.</p>
  
  <p>Here's a string and a <code>gsub</code> that gets rid og all <code>\\</code> even inside double dollars</p>
  
  <pre><code>x &lt;- c("I like \\the big\\ red \\dog\\ $\\hat + \\bar$, here it is $\\bar{x}$",
      "I have $50 to \\spend\\", "$\\frac{4}{5}$ is nice", "$\\30\\ is nice too") 
  
  gsub("\\\\", "\"", x)
  
  ## &gt; gsub("\\\\", "\"", x)
  ## [1] "I like \"the big\" red \"dog\" $\"hat + \"bar$, here it is $\"bar{x}$" 
  ## [2] "I have $50 to \"spend\""    
  ## [3] "$\"frac{4}{5}$ is nice"   
  ## [4] "$\"30\" is nice too"  
  </code></pre>
  
  <p>What I am after is:</p>
  
  <pre><code>## [1] "I like \"the big\" red \"dog\" $\\hat + \\bar$, here it is $\\bar{x}$" 
  ## [2] "I have $50 to \"spend\""
  ## [3] "$\\frac{4}{5}$ is nice"   
  ## [4] "$\"30\" is nice too" 
  </code></pre>