\ 对非转义字符有何作用?
我问了另一个问题,效果很差,所以我会问其他问题。
我在 javascript 'http:\/\/www.site.com\/user'
中有一个字符串。并不是说这是一个带有 '
的文字,所以对于 "
它看起来像 \\/
无论如何我想转义这个字符串,因此问题关于非“特殊”转义字符会发生什么,
另一个问题是如果我有 name:\t me
(或 "name:\\t me"
)函数来转义它,所以有一个选项卡?我正在使用 C# 并且这些字符串来自 JSON 文件
I asked another question poorly so i'll ask something else.
According to http://www.c-point.com/javascript_tutorial/special_characters.htm there are a few escape characters such as \n and \b. However / is not one of them. What happens in this case? (\/
) is the \
ignored?
I have a string in javascript 'http:\/\/www.site.com\/user'
. Not that this is a literal with '
so with "
it would look like \\/
anyways i would like to escape this string thus the question on what happens on non 'special' escape characters.
And another question is if i had name:\t me
(or "name:\\t me"
is there a function to escape it so there is a tab? i am using C# and these strings come from a JSON file
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
根据 Mozilla 的说法:
https://developer.mozilla.org/en/JavaScript/Guide /Values%2c_Variables%2c_and_Literals#section_19
\/
序列未列出,但至少有两种常见用法:<1>需要在使用
/foo/
语法的正则表达式中转义文字斜杠:<2>当您在 HTML 中嵌入 JavaScript 代码时,需要避免无效的 HTML:
...触发器:未打开的元素“P”的结束标记
...没有。
According to Mozilla:
https://developer.mozilla.org/en/JavaScript/Guide/Values%2c_Variables%2c_and_Literals#section_19
The
\/
sequence is not listed but there're at least two common usages:<1> It's required to escape literal slashes in regular expressions that use the
/foo/
syntax:<2> It's required to avoid invalid HTML when you embed JavaScript code inside HTML:
... triggers: end tag for element "P" which is not open
... doesn't.
如果在作为转义序列没有意义的字符之前发现反斜杠,它将被忽略,即
"\/"
和"/"
是相同的字符串JavaScript。/
字符是正则表达式分隔符,因此只需在正则表达式上下文中对其进行转义:最后,如果要将反斜杠后跟的字符折叠到相应的转义序列中,您将必须替换整个表达式:
If a backslash is found before a character which is not meaningful as an escape sequence, it will be ignored, i.e.
"\/"
and"/"
are the same string in Javascript.The
/
character is the regular expression delimiter, so it only has to be escaped in a regex context:Finally, if you want to collapse a backslash followed by a character into the corresponding escape sequence, you'll have to replace the whole expression:
如果 \ + 下一个字符不是转义序列,则 \ 被评估为 \。
例子:
\t->转义序列 t ->选项卡
\\t->转义 \ 和 t -> \t
\\ ->转义序列 \ -> \
\c-> \c (不是转义序列)
\a->转义序列 a -> ???
请注意,完全奇怪的符号上也有转义序列,所以要小心。恕我直言,语言和操作系统之间没有良好的标准。
实际上,它甚至更不标准:在基本 C 中 '\y' -> y + 警告,而不是 \y。所以这非常依赖语言,要小心。 (忽略我下面的评论)。
br,
Juha
编辑:你使用什么语言?= Java 和 c 的行为略有不同。
C 和 java 似乎有相同的转义符,而 python 有不同的转义符:
http://en.csharp-online.net/CSharp_FAQ:_What_are_the_CSharp_character_escape_sequences
http://www.cerritos.edu/jwilson/cis_182/language_resources/java_escape_sequences.htm
http://www.java2s.com/Code/Python/String/EscapeCodesbtnar.htm
\ is evaluated as \ if \ + next character is not an escape sequence.
examples:
\t -> escape sequence t -> tab
\\t -> escape \ and t -> \t
\\ -> escape sequence \ -> \
\c -> \c (not an escape sequence)
\a -> escape sequence a -> ???
Note that there are escape sequences also on completely weird symbols, so be careful. IMHO there is no good standard between languages and operating systems.
And actually, its even more non-stardard: in basic C '\y' -> y + warning, not \y. So this is very language dependent, be careful. (disregard my comment below).
br,
Juha
edit: What language are you using?= Java and c have slightly different behavior.
C and java seem to have the same escapes and python has different:
http://en.csharp-online.net/CSharp_FAQ:_What_are_the_CSharp_character_escape_sequences
http://www.cerritos.edu/jwilson/cis_182/language_resources/java_escape_sequences.htm
http://www.java2s.com/Code/Python/String/EscapeCodesbtnar.htm
在 C# 中,您可以使用反斜杠字符告诉编译器您真正想要什么。但编译后,这些转义字符不存在。
如果您使用
string myString = "\t";
,该字符串实际上将包含一个制表符,而不仅仅是代表一个制表符。您可以通过检查myString.Length
来测试这一点,该长度为 1。但是,如果您想将字符“反斜杠”和“t”发送到 JSON 客户端,则必须告诉编译器保留他的手离开反斜杠,通过转义反斜杠:
string myString = "\\t";
将产生一个由两个字符组成的字符串,即“反斜杠”和“t”。如果您必须跨越多层转义和反转义,事情就会变得混乱,请尝试通过这些层进行调试以查看幕后到底发生了什么。
In C# you can use the backslash character to tell the compiler what you really want. After compiling though, these escape characters do not exist.
If you use
string myString = "\t";
the string will actually contain a TAB character, not just represent one. You can test this by checkingmyString.Length
which is 1.If you want to send the characters "backslash" and "t" to your JSON client however, you'll have to tell the compiler to keep his hands off the backslash, by escaping the backslash:
string myString = "\\t";
will result in a string of two characters, the "backslash" and the "t".Things get messy if you have to cross multiple layers of escaping and unescaping, try to debug through these layers to see what's really happening under the hood.