title: replacing characters with escape in a javascript string
body: <p>i know there must be thousands of threads like this, but the internet and stack overflow is flooded with results from other programming languages and simply replacing the characters with  a whitespace and so on...</p>
  
  <p>so... my problem is as follows...</p>
  
  <p>i have a form with: <code>&lt;input type='text' id='my_input'&gt;</code></p>
  
  <p>then i parse all my fields in javascript (with jquery) to create my ajax call, like this</p>
  
  <p><code>var my_value = $('#my_input').val();</code></p>
  
  <p>then i do a replace to get the line breaks <code>my_value = my_value.replace('\n', '&lt;br&gt;');</code></p>
  
  <p>after that i post my variables with ajax like this:</p>
  
  <pre><code>data_to_post = 'my_field1=' + my_value1 + 'my_field2' + my_value2;
  
  $.ajax({  
  type: 'POST',  
  url: '/write_to_db.php',  
  data: data_to_post
  });  
  </code></pre>
  
  <p>now  many people on my site talk about programming and that content started screwing up my site, because their post content gets parsed as php or as html formatting - i'm planning to replace all the problematic characters that could screw up the code of my site.</p>
  
  <p>the most problematic expressions are html tags and &amp;= because it interferes with the way i put my post variable together.</p>
  
  <p>now, because those characters are necessary for the content, i can't simply replace them all with with a single character.</p>
  
  <p>is there any way i could escape them (at best in a one line command)?</p>
  
  <p>thanks!</p>
  
  <p>UPDATE:
  my temporary solution is this:</p>
  
  <pre><code>this_string = this_string.replace(/&lt;/g, '&amp;#60;');
  this_string = this_string.replace(/&gt;/g, '&amp;#62;');
  this_string = this_string.replace(/\n/g, '&lt;br&gt;');
  this_string = escape(this_string);
  </code></pre>