为什么在字符串中使用javascript转义字符的引号需要是\\'而不是\'

Sar*_*nyu 5 javascript c# asp.net quotes escaping

我只是在使用javascript的问题我正在使用asp.net上的代码,经过几个小时的计算后,它变成了逃避字符的问题.

起初我用这个.

ScriptManager.RegisterStartupScript(this, this.GetType(), "temp", "alert('Can't delete this data because it is bound with rate plan');", true);
Run Code Online (Sandbox Code Playgroud)

这将导致javascript错误,因为"不能"的引用需要使用转义字符,所以我使用.

ScriptManager.RegisterStartupScript(this, this.GetType(), "temp", "alert('Can\'t delete this data because it is bound with rate plan');", true);
Run Code Online (Sandbox Code Playgroud)

但它仍然无效.

我终于用了

ScriptManager.RegisterStartupScript(this, this.GetType(), "temp", "alert('Can\\'t delete this data because it is bound with rate plan');", true);
Run Code Online (Sandbox Code Playgroud)

这很好.

我只是好奇为什么我们需要使用\\'而不是\'为了使转义字符正常工作.

Que*_*tin 7

\是C# JavaScript中的转义字符.

当你给C#时"\'",创建一个包含撇号的字符串.

当你给C#"\\'"然后第一个\转义第二个\(所以第二个\不被视为转义字符)并被'视为一个普通'(因为字符串没有分隔'.