title: Java replaceAll() method to escape special characters
body: <p>I am using java <code>replaceAll()</code> method to escape new line characters</p>
  
  <pre><code>String comment = "ddnfa \n  \r \tdnfadsf ' \r t  ";
  comment = comment.replaceAll("(\\n|\\r|\\t)","\\\\$1");
  System.out.println(comment);
  </code></pre>
  
  <p>But the above code is still inserting new line.</p>
  
  <p>Is there a way to output the comment exactly the same (i.e. with <code>\n</code> and <code>\r</code> instead of inserting new line)?</p>
  
  <p><strong>UPDATE:</strong> </p>
  
  <p>I ended up using:</p>
  
  <pre><code>comment = comment.replaceAll("\\n","\\\\n")
                   .replaceAll("\\r","\\\\r")
                   .replaceAll("\\t","\\\\t");
  </code></pre>