为什么在字符串中使用JavaScript转义字符需要是\\'而不是 \'
我只是在 asp.net 上的代码后面使用 javascript 时遇到问题,经过几个小时的弄清楚,原来是转义字符的问题。
一开始我用这个。
ScriptManager.RegisterStartupScript(this, this.GetType(), "temp", "alert('Can't delete this data because it is bound with rate plan');", true);
这会导致javascript错误,因为“can't”处的引号需要使用转义字符,所以我使用。
ScriptManager.RegisterStartupScript(this, this.GetType(), "temp", "alert('Can\'t delete this data because it is bound with rate plan');", true);
但它仍然不起作用。
最后我用了
ScriptManager.RegisterStartupScript(this, this.GetType(), "temp", "alert('Can\\'t delete this data because it is bound with rate plan');", true);
,效果很好。
我只是好奇为什么我们需要使用 \\'
而不是 \'
以使转义字符正常工作。
I just having a problems with javascript i am using on code behind on asp.net, after a few hour of figuring it out it turn out to be the problem of escape character.
At first i use this.
ScriptManager.RegisterStartupScript(this, this.GetType(), "temp", "alert('Can't delete this data because it is bound with rate plan');", true);
This will made javascript error because quotation at "can't" need to use escape character so i use.
ScriptManager.RegisterStartupScript(this, this.GetType(), "temp", "alert('Can\'t delete this data because it is bound with rate plan');", true);
but it still not work.
at last i use
ScriptManager.RegisterStartupScript(this, this.GetType(), "temp", "alert('Can\\'t delete this data because it is bound with rate plan');", true);
and it is fine.
i am just curious why we need to use \\'
instead of \'
in order to make escape character works correctly.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
\
是 C# 中的转义字符 和 JavaScript 中的 。当您给 C#
"\'"
时,会创建一个包含撇号的字符串。当您给出 C#
"\\'"
时,第一个\
会转义第二个\
(因此第二个\
不被视为转义字符),并且'
被视为普通'
(因为该字符串不是用'
分隔的。\
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'
.在 ac# 字符串中,
\
需要转义,因为它是\n
等内容的特殊前缀。您可能会发现逐字使用更容易strig 文字,不需要转义("
到""
除外)。例如:
注意前导
@
在字符串文字之前,表示替代转义规则的使用,这也允许直接在字符串中使用换行符等,即。In a c# string,
\
needs to be escaped, as it is a special prefix for things like\n
etc. You may find it easier to use a verbatim strig literal, which doesn't need escaping (except for"
to""
).For example:
Note the leading
@
before the string literal, which indicates the usage of the alternative escaping rules. This also allows newlines etc directly in the string, i.e.因为“\”也是 C# 的转义字符。
我更喜欢在字符串的开头(就在字符串开始之前)使用 @ 特殊运算符,因为它告诉 C# 它不能处理转义字符。
例如:
无论如何,我找不到单引号的意义。您可以通过使用双引号字符串表示法来避免转义这个单引号:
如果我不记得有很多 PHP 编码人员贡献了脚本,我就不明白 JavaScript 中单引号的滥用,因为这种语言的行为方式是根据单引号或双引号字符串的不同方式。
无论如何,您可以检查有关 JavaScript 中单引号和双引号的其他问题:
Because "\" is the escaping character for C# too.
I'd prefer to use @ special operator at the beggining of your string, just before it starts it, because it tells C# that it mustn't process escaping characters.
For example:
Anyway, I don't find the point of a single quot. You can avoid escaping this single quot by using double-quot string notation:
I don't understand the abuse of single quot in JavaScript if I don't remember there're a lot of PHP coders contributing scripts, since this language behaves in a different way depending of single or double-quoted strings.
Anyway, you can check this other question about single and double-quoting in JavaScript:
当您使用 \\ 时,它会在实际的 javascript 中转义为 \ ,从而转义该字符。你本质上是在逃避两次
When you use \\ it escapes to \ in the actual javascript which escapes the character. You are essentially escaping twice
名称中的单引号和撇号(例如 O'Brian)通常会在动态客户端脚本中引起麻烦,因为它们会破坏它们并允许插入恶意代码(也称为脚本攻击)。
我为代码隐藏编写了以下C#6扩展方法来解决这个问题:
它的用法很简单。请考虑以下动态脚本示例:
注意,
nameEscp
已被单引号括起来,因此您可以安全地将其放在=
之后。诀窍在于,字符串会被转义,并且在赋值时立即立即转义(通过执行 JavaScript 表达式),即,
将作为插入的赋值表达式将作为脚本发送到客户端。执行后,
.value
包含O'Brian
。Single quotes and apostrophes in names (such as O'Brian) are usually causing trouble in dynamic client scripts, because they'll break them and allow to insert malicious code (aka scripting attacks).
I have written the following C#6 extension method for code-behind to solve this:
Its usage is simple. Consider the following dynamic script example:
Note that
nameEscp
is already surrounded by single quote so you can safely place it after the=
.The trick is that the string is escaped and upon assignment immediately unescaped (by executing a JavaScript expression) on the fly, i.e.
will be the inserted assignment expression which will be sent to the client as script. After execution,
.value
containsO'Brian
.