Javascript变量缺少字符

时间:2017-10-05 16:01:53

标签: javascript asp.net

我正在从ASP.net evaluation <- mutate(eval_of_sequence= ifelse(grepl("1,2,3,4",condensed_data$`concatenated`),"following proper sequence", ifelse(grepl("1,2,3",condensed_data$`concatenated`),"following proper sequence", ifelse(grepl("1,2",condensed_data$`concatenated`),"following proper sequence", ifelse(grepl("1",condensed_data$`concatenated`),"following proper sequence", "breaking proper sequence")) 1. Baker Sequence_concatenated evaluation 2. John 1 following proper sequence 3. Jack 1,3,2 breaking proper sequence 4. Jane 2,1 breaking proper sequence 5. Jill 2 following proper sequence

设置一个javascript变量
<%=HttpContext.Current.User.Identity.Name%>

输出&#34;域名服务器&#34;

基本上它会取出用户名的反斜杠和第一个字母。为什么删除这两个字符?

2 个答案:

答案 0 :(得分:1)

你正在做的事情实际上是标记:

var username = 'domain\username';

\u被视为JavaScript字符串中的转义序列。根据用户名的实际情况,您会看到不同的结果,从换行符到标签,再到语法错误。

您需要使用HttpUtility.JavaScriptStringEncode

之类的内容在服务器上对其进行正确编码
var username = '<%=HttpUtility.JavaScriptStringEncode(HttpContext.Current.User.Identity.Name)%>';

答案 1 :(得分:0)

在JS(许多其他语言)中,\用于具有特殊角色的字符串,称为 ESCAPE CHARACTER 。您需要使用\\在字符串中获取\,在控制台中对其进行测试:

console.log("\");
console.log("\\");

这就是你如何为换行等形成\n之类的特殊字符。

我认为这些都是在JS中使用转义字符的情况:

\'  single quote
\"  double quote
\\  backslash
\b  Backspace
\r  Carriage Return
\f  Form Feed
\t  Horizontal Tabulator
\v  Vertical Tabulator

长话短说,JS认为\character是字符串中的一个符号(而不是\和后面的字符),\\代表\