title: What is the standard approach for escaping harmful characters / sanitizing user's input with JS?
body: <p>After reading tons of articles and StackOverflow responses I still don't get what is the industry standard approach for dealing with user's inputs (textarea and input type="text"). What and how should I escape chars? What and how should be stripped / replaced so JSON will not break, especially when the user can copy-paste some of the weirdest characters ever?</p>
  
  <p>The doubtful solution that I have put in place a long time ago was:</p>
  
  <pre><code>String.prototype.escapeChars = function () {
   return this.replace(/\&amp;/g, '&amp;#x26;')
  .replace(/\n\r?/g, '&amp;#13;')
  .replace(/\n/g, '&amp;#13;')
  .replace(/\r/g, '&amp;#13;')
  .replace(/\s/g, '&amp;#32;')
  .replace(/\&lt;/g, '&amp;#x3c;')
  .replace(/\"/g, '&amp;#34;')
  .replace(/\'/g, '&amp;#39;')
  .replace(/\&gt;/g, '&amp;#x3e;')
  .replace(/\\/g, '&amp;#x27;')
  .replace(/[^\x00-\x7F]/g, '');
  };
  </code></pre>
  
  <p>I would tremendously appreciate if someone will explain in detail or point to a good tutorial on how it should be done <strong>PROPERLY</strong> with vanilla js. </p>