title: fs.writeFileSync Doesn't Write String With Escaping Backslashes
body: <p>In my program I'm trying to write a string that represents a regular expression exactly as it is written into a file. This means preserving <strong>any</strong> backslashes that occur in the string. However, when I try to write this string to a file, each pair of backslashes has one less backslash than it should.</p>
  
  <p>My example to demonstrate this problem is like so:</p>
  
  <pre><code>let filepath = path.join(__dirname, 'output.js');
  let outputString = "\\d{3}(-|\\s)?\\d{3}(-|\\s)?\\d{4}";
  fs.writeFileSync(filepath, outputString, 'utf-8');
  </code></pre>
  
  <p>The output result is:</p>
  
  <pre><code>\d{3}(-|\s)?\d{3}(-|\s)?\d{4}
  </code></pre>
  
  <p>Is there some option I can pass to <code>fs.writeFileSync</code> to maintain all characters in the string to write to the file?</p>
  
  <p>I realize one solution might involve escaping all possible characters in this string with a library like <a href="https://www.npmjs.com/package/js-string-escape" rel="nofollow noreferrer">js-string-escape</a>, but since these particular strings represent regular expressions is it <em>really</em> safe to do so? I don't want to alter them or their functionality in any way.</p>
  
  <p><em>note:</em> I realize this output isn't really valid JavaScript, but it's a somewhat contrived example of what I am really trying to do. This small snippet of code just best demonstrates the issue at hand.</p>