最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

string - typing a single forward slash in javascript - Stack Overflow

programmeradmin0浏览0评论

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
 |  Show 9 more ments

2 Answers 2

Reset to default 3

The 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 = "\\";
发布评论

评论列表(0)

  1. 暂无评论