Javascript - 如何在字符串中显示转义字符? [重复]

时间:2022-05-22 22:29:07

This question already has an answer here:

这个问题在这里已有答案:

Very simple question, but for some reason I can't find the answer anywhere after 10 minutes of Googling. How can I show escape characters when printing in Javascript?

很简单的问题,但由于某种原因,我在谷歌搜索10分钟后无法找到答案。如何在Javascript中打印时显示转义字符?

Example:

str = "Hello\nWorld";
console.log(str);

Gives:

Hello
World

When I want it to give:

当我想要它给予:

Hello\nWorld

3 个解决方案

#1


54  

If your goal is to have

如果你的目标是

str = "Hello\nWorld";

and output what it contains in string literal form, you can use JSON.stringify:

并以字符串文字形式输出它包含的内容,您可以使用JSON.stringify:

console.log(JSON.stringify(str)); // ""Hello\nworld""

console.log adds the outer quotes (at least in Chrome's implementation), but the content within them is a string literal.

console.log添加外引号(至少在Chrome的实现中),但其中的内容是字符串文字。

#2


14  

You have to escape the backslash, so try this:

你必须逃避反斜杠,所以试试这个:

str = "Hello\\nWorld";

Here are more escaped characters in Javascript.

以下是Javascript中更多转义的字符。

#3


13  

JavaScript uses the \ (backslash) as an escape characters for:

JavaScript使用\(反斜杠)作为转义字符:

  • \' single quote
  • 单引号

  • \" double quote
  • “双引号

  • \ backslash
  • \n new line
  • \ n新行

  • \r carriage return
  • \ r \ n回车

  • \t tab
  • \b backspace
  • \f form feed
  • \ f表格饲料

  • \v vertical tab (IE < 9 treats '\v' as 'v' instead of a vertical tab ('\x0B'). If cross-browser compatibility is a concern, use \x0B instead of \v.)
  • \ v vertical tab(IE <9将'\ v'视为'v'而不是垂直制表符('\ x0B')。如果需要考虑跨浏览器兼容性,请使用\ x0B而不是\ v。)

  • \0 null character (U+0000 NULL) (only if the next character is not a decimal digit; else it’s an octal escape sequence)
  • \ 0 null字符(U + 0000 NULL)(仅当下一个字符不是十进制数字时;否则它是八进制转义序列)

Note that the \v and \0 escapes are not allowed in JSON strings.

请注意,JSON字符串中不允许使用\ v和\ 0转义符。

#1


54  

If your goal is to have

如果你的目标是

str = "Hello\nWorld";

and output what it contains in string literal form, you can use JSON.stringify:

并以字符串文字形式输出它包含的内容,您可以使用JSON.stringify:

console.log(JSON.stringify(str)); // ""Hello\nworld""

console.log adds the outer quotes (at least in Chrome's implementation), but the content within them is a string literal.

console.log添加外引号(至少在Chrome的实现中),但其中的内容是字符串文字。

#2


14  

You have to escape the backslash, so try this:

你必须逃避反斜杠,所以试试这个:

str = "Hello\\nWorld";

Here are more escaped characters in Javascript.

以下是Javascript中更多转义的字符。

#3


13  

JavaScript uses the \ (backslash) as an escape characters for:

JavaScript使用\(反斜杠)作为转义字符:

  • \' single quote
  • 单引号

  • \" double quote
  • “双引号

  • \ backslash
  • \n new line
  • \ n新行

  • \r carriage return
  • \ r \ n回车

  • \t tab
  • \b backspace
  • \f form feed
  • \ f表格饲料

  • \v vertical tab (IE < 9 treats '\v' as 'v' instead of a vertical tab ('\x0B'). If cross-browser compatibility is a concern, use \x0B instead of \v.)
  • \ v vertical tab(IE <9将'\ v'视为'v'而不是垂直制表符('\ x0B')。如果需要考虑跨浏览器兼容性,请使用\ x0B而不是\ v。)

  • \0 null character (U+0000 NULL) (only if the next character is not a decimal digit; else it’s an octal escape sequence)
  • \ 0 null字符(U + 0000 NULL)(仅当下一个字符不是十进制数字时;否则它是八进制转义序列)

Note that the \v and \0 escapes are not allowed in JSON strings.

请注意,JSON字符串中不允许使用\ v和\ 0转义符。