title: outputting valid Javascript from Java
body: <p>I'm using Java 6.  I want to output code for a Javascript variable ...</p>
  
  <pre><code>                    String jsValue = escapeForJS(value);
                      String expression = "storedVars['myVar'] = \"" + jsValue + "\";";
  </code></pre>
  
  <p>Is there anything standard that will do this?  I came up with my own
  function, but would prefer to use something tried and true.</p>
  
  <pre><code>    /* creates a JS expression that can be used within quotes. */
      private String escapeForJS(String value) {
              value = value.replace("\n", "\\n");
              value = value.replace("\r", "\\r");
              value = value.replace("\"", "\\\"");
              return value;
      }
  </code></pre>
  
  <ul>
  <li>Dave </li>
  </ul>