title: Regex to escape special characters in Groovy
body: <p>I'm trying to use a regex to scape special characters, right now used it on Java and works perfect, it does exactly what I want <code>Scape any special character</code> however I tried this in Groovy but the same line doesn't work, as far I investaged it's because <code>$</code> is reserved in Groovy, so far I tried this;</p>
  
  <p>Java: (Does the job)</p>
  
  <pre><code>String specialCharRegex = "[\\W|_]";
  ...
  term = term.replaceAll(specialCharRegex, "\\\\$0");
  ...
  </code></pre>
  
  <p>Groovy:</p>
  
  <p>error</p>
  
  <pre><code>String specialCharRegex = "[\\W|_]";
  ...
  term = term.replaceAll(specialCharRegex, "\\\\$0");
  ...
  </code></pre>
  
  <p>error</p>
  
  <pre><code>String specialCharRegex = "[\\W|_]";
  ...
  term = term.replaceAll(specialCharRegex, "\\\\\$0");
  ...
  </code></pre>
  
  <p>error</p>
  
  <pre><code>String specialCharRegex = "[\\W|_]";
  ...
  term = term.replaceAll(specialCharRegex, '\\\\$0');
  ...
  </code></pre>
  
  <p>error</p>
  
  <pre><code>String specialCharRegex = "[\\W|_]";
  ...
  term = term.replaceAll(specialCharRegex, '\\\\$1');
  ...
  </code></pre>
  
  <p>I use <code>https://groovyconsole.appspot.com/</code> to test it.</p>
  
  <p>Output in Groovy should be:</p>
  
  <pre><code>Input: test 1&amp; test
  Output: test 1\&amp; test
  
  Input: test 1&amp; test 2$
  Output: test 1\&amp; test 2\$
  
  Input: test 1&amp; test 2$ test 3%
  Output: test 1\&amp; test 2\$ test 3\%
  
  Input: !"@#$%&amp;/()=?
  Output: \!\"\@\#\$\%\&amp;\/\(\)\=\?
  </code></pre>