Escape quotes in JavaScript

You need to escape the string you are writing out into DoEdit to scrub out the double-quote characters. They are causing the onclick HTML attribute to close prematurely. Using the JavaScript escape character, \, isn’t sufficient in the HTML context. You need to replace the double-quote with the proper XML entity representation, ".

Can I convert a C# string value to an escaped string literal?

I found this: private static string ToLiteral(string input) { using (var writer = new StringWriter()) { using (var provider = CodeDomProvider.CreateProvider(“CSharp”)) { provider.GenerateCodeFromExpression(new CodePrimitiveExpression(input), writer, null); return writer.ToString(); } } } This code: var input = “\tHello\r\n\tWorld!”; Console.WriteLine(input); Console.WriteLine(ToLiteral(input)); Produces: Hello World! “\tHello\r\n\tWorld!”

What is the HtmlSpecialChars equivalent in JavaScript?

There is a problem with your solution code–it will only escape the first occurrence of each special character. For example: escapeHtml(‘Kip\’s <b>evil</b> “test” code\’s here’); Actual: Kip&#039;s &lt;b&gt;evil</b> &quot;test” code’s here Expected: Kip&#039;s &lt;b&gt;evil&lt;/b&gt; &quot;test&quot; code&#039;s here Here is code that works properly: function escapeHtml(text) { return text .replace(/&/g, “&amp;”) .replace(/</g, “&lt;”) .replace(/>/g, “&gt;”) .replace(/”/g, … Read more

What is the recommended way to escape HTML symbols in plain Java?

StringEscapeUtils from Apache Commons Lang: import static org.apache.commons.lang.StringEscapeUtils.escapeHtml; // … String source = “The less than sign (<) and ampersand (&) must be escaped before using them in HTML”; String escaped = escapeHtml(source); For version 3: import static org.apache.commons.lang3.StringEscapeUtils.escapeHtml4; // … String escaped = escapeHtml4(source);