如何将我的C#代码转换为js方法替换

亚历克斯

在C#中我有这行代码

this.Office.Name.Replace(@"""", @"\""")

我想在js中使用此字符串

我这样尝试过:

  1. var test = <%= this.Office.Name.Replace(@“”“”,@“ \”“”); %>;
    编译器错误消息:CS1026:)预期的第574行:
    字符串测试= <%= this.Office.Name.Replace(@“”“”,@“ \”“”); %>;

  2. 没有 @

    字符串测试= <%= this.Office.Name.Replace(“”“”,“ \”“”); %>;

    编译器错误消息:CS1010:常量中的换行符

  3. 字符串测试= <%= this.Office.Name.Replace('“','\”'); %>;

    编译器错误消息:CS1026:)

Winner_Joiner

由于它是用于声明变量的javascript var(反斜杠应该起作用)

var test = <%=this.Office.OfficeName.Replace("\"", "\\\"") %>;  
// NO SEMICOLON in the asp.net TAGS <%= ... %>
// 3 Backslashes the first one escapes the Second one, 
//    the Second is literal(since it was escaped),
//    the third escapes the double quot.

但是我会看一下可能的值,以确保没有任何极端情况,在这种情况下这种转换可能行不通。

如果要使用Javascript而不是c#进行替换,请使用Pid的答案

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章