admin管理员组文章数量:1432196
I want to generate the string "\" in Javascript but couldn't seem to do it. If I only write "\", I would get pile time error because " itself is escaped. But if I do "\\", I would get two slashes as the output. So how do I generate a string with a single forward slash?
I want to generate the string "\" in Javascript but couldn't seem to do it. If I only write "\", I would get pile time error because " itself is escaped. But if I do "\\", I would get two slashes as the output. So how do I generate a string with a single forward slash?
Share Improve this question asked Jun 30, 2014 at 17:16 user3727864user3727864 412 silver badges5 bronze badges 14-
3
I might be misunderstanding, but a forward-slash (monly just 'slash') is
/
, you seem to be using the back-slash? – David Thomas Commented Jun 30, 2014 at 17:17 - @DavidThomas I might got the term wrong then. But whatever the name is, I want generate "\" as the output – user3727864 Commented Jun 30, 2014 at 17:19
-
Did you try
"\\"
? Because that should work (it escapes the escape character). – David Thomas Commented Jun 30, 2014 at 17:20 -
1
Why do you think that
"\\"
would produce two backslashes? (NB: not forward slashes) – Alnitak Commented Jun 30, 2014 at 17:21 -
2
@user3727864 If the output is JSON-formatted, it requires backslashes to be escaped as well, and will write them as such. The escape sequence, though, should still represent a single character in the value. Example:
console.log('\\'.length); // 1
– Jonathan Lonowski Commented Jun 30, 2014 at 17:40
2 Answers
Reset to default 3The character /
is a slash. The character \
is a backslash.
Backslash \
is used as an escape character for strings in JavaScript, and in JSON. It is required for some characters to remove ambiguity from string literals. This string is ambiguous:
'He's going to the park'
There are three single quote '
marks, and the parser doesn't know what is part of the string and what isn't. We can use a backslash to escape the one that we want to represent the character '
instead of the close of the string literal (also '
).
'He\'s going to the park'
Now, if the backslash has special meaning, how do we represent a literal backslash \
character in the string? By simply escaping the backslash \
with a backslash \
.
'C:\\DOS\\mand.' // In memory this is: C:\DOS\mand.
Remember that this escaping is only for the text representation of strings in code or JSON. The code is parsed and the strings in memory are what we would expect, with all escaping resolved to the proper characters.
Now your question asks about JSON and makes the assumption that this is incorrect:
I am writing '\' as the key to a JSON package. The result is something like
"READY_TO_PRINT_DATE":"/\\Date(1403911292:981000+420)\\/"
.
JSON requires the same escaping as you find in JavaScript, and for the same reason... to remove ambiguity from strings. The JSON-version of the string /\\Date(1403911292:981000+420)\\/
is how you would properly represent the actual string /\Date(1403911292:981000+420)\/
.
I hope this helps clears up some of your confusion.
you can escape the slash:
myvar = "\\";
本文标签: stringtyping a single forward slash in javascriptStack Overflow
版权声明:本文标题:string - typing a single forward slash in javascript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745571533a2664076.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论