Escape Quote in C# for JavaScript Consumption

JavaScript - Escape single quote in the JavaScript Object property value

The first line of code you show is not valid JavaScript:

var fileUploadDic = { 'firstname': 'Jo''hn', 'lastname' : 'Macy' , 'country' : 'USA };

Because the string 'Jo''hn' can't have single quotes in it like that. It needs to be either 'Jo\'hn' or "Jo'hn". That is to say that if your JS string is quoted with single quotes you have to escape each single quote character within the string with a backslash, but if your JS string is quoted with double quotes you can use singles freely within the string. If you want two single quotes in a row 'Jo\'\'hn' or "Jo''hn".

There is nothing you can do to fix this client-side, because being invalid JavaScript it won't run.

You have to fix it server-side; the easiest way is probably to escape it with a backslash, noting that if that is in a C# string you'll need to escape the backslash too so that the actual output to the browser contains a backslash:

"var fileUploadDic = { 'firstname': 'Jo\\'hn', 'lastname' : 'Macy' , 'country' : 'USA };"

Single Quote Escape Character Formatting for JavaScript

If you want to just escape apostrophes in JavaScript you could try to simply replace them with \’:

s = s.replace("'", "\'");

It won’t affect your further work with this string so if you write it to the console it will output a result without backslash:

var s = "De'Leon";
s = s.replace("'", "\'");
console.log(s); // > De'Leon

escape double quotes inside string literal in C#

You should not use regular expressions to escape your C# strings into a JavaScript friendly / safe format. Instead, assuming you are using .NET 4, you can use HttpUtility.JavaScriptStringEncode and it's overload to take care of both single and double quotes for you.

For example:

string UserMsg = GetMessageText(999);
StringBuilder script = new StringBuilder();
script.AppendFormat("var UserMsg =\"{0}\";{1}", HttpUtility.JavaScriptStringEncode(UserMsg), Environment.NewLine);
ScriptManager.RegisterClientScriptBlock(this, GetType(), "scriptparams", script.ToString(),true);

Would ouput the following with UserMsgset to "we found a "problem" in your details":

var UserMsg ="we found a \"problem\" in your details";

Escape double quotes in a string

No.

Either use verbatim string literals as you have, or escape the " using backslash.

string test = "He said to me, \"Hello World\" . How are you?";

The string has not changed in either case - there is a single escaped " in it. This is just a way to tell C# that the character is part of the string and not a string terminator.

how to convert escaped c# string with double quotes to escaped javascript string with double quotes

Typically it's best to use your favorite JSON serializer to encode any value that you want to inject into javascript. That will automatically add double-quotes around the string, and escape anything inside the string appropriately:

test(@Html.Raw(JsonConvert.Serialize(lol)));

why using javascript escape character of quotation in string need to be \\' instead of \'

\ is an escape character in C# and in JavaScript.

When you give C# "\'" is creates a string containing an apostrophe.

When you give C# "\\'" then the first \ escapes the second \ (so the second \ isn't treated as an escape character) and the ' is treated as a plain ' (because the string is not delimited with '.

Escaping a double-quote in inline c# script within javascript

Call HttpUtility.JavaScriptStringEncode.

This method is new to ASP.Net 4.0; for earlier versions, use the WPL.

How do I escape a single quote ( ' ) in JavaScript?

You should always consider what the browser will see by the end. In this case, it will see this:

<img src='something' onmouseover='change(' ex1')' />

In other words, the "onmouseover" attribute is just change(, and there's another "attribute" called ex1')' with no value.

The truth is, HTML does not use \ for an escape character. But it does recognise " and ' as escaped quote and apostrophe, respectively.

Armed with this knowledge, use this:

document.getElementById("something").innerHTML = "<img src='something' onmouseover='change("ex1")' />";

... That being said, you could just use JavaScript quotes:

document.getElementById("something").innerHTML = "<img src='something' onmouseover='change(\"ex1\")' />";


Related Topics



Leave a reply



Submit