title: JSON stringify unable to escape single quote using replacer function
body: <p>I'm trying to replace single quotes in my json string values with \' however it is not working only when I try to use the replacer function. </p>
  
  <pre><code>var myObj = {
  test: "'p'"
  }
  
   var replacer = function (key, value) {
                  if (typeof value === 'string')
                      return value.replace(/'/g, "\\'");
                  else return value;
              };
  
    var JSONstring = JSON.stringify(myObj, replacer, ' ');
  
    alert(JSONstring);
  </code></pre>
  
  <p><a href="https://jsfiddle.net/4fsqozek/1/" rel="nofollow noreferrer">https://jsfiddle.net/4fsqozek/1/</a></p>
  
  <p>However if I do just a simple replace after the string is created like this without using the replacer function</p>
  
  <pre><code> var JSONstring = JSON.stringify(myObj).replace(/'/g, "\\'");
  </code></pre>
  
  <p>The regex I used works fine.</p>
  
  <p><strong>EDIT</strong> - clarification - using replacer function the output value contains double backslash like this \\'p\\' , which is not what I'm expecting</p>
  
  <p>Can anyone explain this?</p>