Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>My answer makes some assumptions, as I've had to fill in the rather sizeable gaps in your question:</p> <ul> <li>The user will enter a text string into a textbox;</li> <li>Your script will read the textbox contents, and use those contents as the <em>value</em> of one of the items in a JSON string that it's building;</li> <li>The script sends this resulting JSON string to the server somehow.</li> </ul> <p>If I've got that right, let's proceed...</p> <hr> <h2>Baseline code</h2> <p>So, with some placeholders, you're doing:</p> <pre><code>function get_contents_of_textbox() { // Dummy data for example return 'My mum pushed and I said "Hello World"!'; } function send_to_server(json_str) { // Dummy action: display JSON string console.log(json_str); } var myVal = get_contents_of_textbox(); var JSON = '{ "test": "' + myVal + '" }'; send_to_server(JSON); </code></pre> <h3><a href="http://jsfiddle.net/EgKzs/" rel="noreferrer">Live demo</a>, showing the malformed JSON.</h3> <hr> <h2>Initial attempt</h2> <p>To ensure that <code>JSON</code> is valid, <em>escape</em> any quotes and backslashes that it may contain. You already gave it a go:</p> <pre><code>myVal = myVal.replace('"', "\\\""); </code></pre> <p>and <a href="http://jsfiddle.net/xqyCp/" rel="noreferrer">the result of your attempt</a> is:</p> <pre><code>{ "test": "My mum pushed and I said \"Hello World"!" } </code></pre> <p>Only the first quote has been escaped. This is because only one instance of the search string is replaced by default.</p> <p>The Mozilla documentation <a href="https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/String/replace" rel="noreferrer">says</a>:</p> <blockquote> <p>To perform a global search and replace, either include the g flag in the regular expression or if the first parameter is a string, include g in the flags parameter.</p> </blockquote> <hr> <h2>Working attempt</h2> <p>Unfortunately, the <code>flags</code> parameter is non-standard, so let's switch to the regex version of <code>replace</code>, and use the <code>/g</code> switch in it:</p> <pre><code>myVal = myVal.replace(/"/g, '\\"'); </code></pre> <p><sup>(You'll notice that I also condensed the replacement string, for brevity.)</sup></p> <p>Result:</p> <pre><code>{ "test": "My mum pushed and I said \"Hello World\"!" } </code></pre> <h3><a href="http://jsfiddle.net/NY9jy/" rel="noreferrer">Live demo.</a> Hurrah!</h3> <hr> <h2>Complete solution</h2> <p>Let's also add logic to escape backslashes, and we end up with this:</p> <pre><code>function get_contents_of_textbox() { // Dummy data for example return 'My mum pushed [a back\\slash] and I said "Hello World"!'; } function send_to_server(json_str) { // Dummy action: display JSON string console.log(json_str); } var myVal = get_contents_of_textbox(); myVal = myVal.replace(/\\/g, '\\\\'); // escape backslashes myVal = myVal.replace(/"/g, '\\"'); // escape quotes var JSON = '{ "test": "' + myVal + '" }'; send_to_server(JSON); </code></pre> <p>Result:</p> <pre><code>{ "test": "My mum pushed [a back\\slash] and I said \"Hello World\"!" } </code></pre> <h3><a href="http://jsfiddle.net/czs4S/" rel="noreferrer">Live demo.</a></h3>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload