console.log()中的反斜杠"\"未出现 [英] Backslash '\' in console.log() not appearing
问题描述
我正在尝试在console.log()
和<p></p>
中使用反斜杠,但是似乎在页面加载时,所有反斜杠都被删除了.
I'm trying to use a back slash in console.log()
and within <p></p>
but it seems that when the page loads, all back slashes are removed.
JS示例
console.log('\m/ Lets rock. \m/');
结果
m/ Lets rock. m/
如何防止将其删除?
编辑:反斜杠不是正斜杠.在layout.jade
的<head>
标记内的express.上在node.js上运行此代码.反斜杠在REPL中可见,但在Web浏览器(Chrome& Firefox)的节点上运行时不可见.
Backslash not forward slash. Running this on node.js with express, within the <head>
tags of layout.jade
. Backslash visible in REPL, but not when running on node in the web browser (Chrome & Firefox).
推荐答案
如果您的结果是m/ Lets rock. m/
,我会看到正斜杠.
If m/ Lets rock. m/
is your result, I see forward slashes.
如果您的意思是反斜杠,请对其进行转义以表明该字符串需要文字反斜杠:
If you mean backslashes, escape them to show that the string wants a literal backslash:
console.log('\\m/ Lets rock. \\m/');
否则,JavaScript会将其解释为\m
EscapeSequence .这就是为什么您需要\\
EscapeSequence .
Otherwise, JavaScript interprets this as a \m
EscapeSequence. That's why you need the \\
EscapeSequence.
解决方案是反斜杠需要两次转义:
The solution was that the backslashes needed to be double escaped:
console.log('\\\\m/ Lets rock. \\\\m/');
...显然,反斜杠被当作转义字符处理了两次(一次在创建初始字符串时,然后又出于其他目的).
...apparently the backslashes are processed as escaped characters twice (once in the initial string creation, then again for some other purpose).
因此,字符串创建为我们提供了:
So the string creation gives us:
'\\m/ Lets rock. \\m/'
...随后的处理结果为:
...then the subsequent processing results in:
'\m/ Lets rock. \m/'
这篇关于console.log()中的反斜杠"\"未出现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!