\ 对非转义字符有何作用?

发布于 2024-10-06 15:12:00 字数 705 浏览 6 评论 0原文

问了另一个问题,效果很差,所以我会问其他问题。

根据 http://www.c-point.com/javascript_tutorial/special_characters.htm< /a> 有一些转义字符,例如 \n 和 \b。然而 / 不是其中之一。在这种情况下会发生什么? (\/) \ 是否被忽略?

我在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

ま柒月 2024-10-13 15:12:00

根据 Mozilla 的说法:

对于未列出的字符 [...] 前面的反斜杠将被忽略,但不推荐使用这种用法
应该避免。

https://developer.mozilla.org/en/JavaScript/Guide /Values%2c_Variables%2c_and_Literals#section_19

\/ 序列未列出,但至少有两种常见用法:

<1>需要在使用 /foo/ 语法的正则表达式中转义文字斜杠:

var re = /^http:\/\//;

<2>当您在 HTML 中嵌入 JavaScript 代码时,需要避免无效的 HTML:

<script type="text/javascript"><!--
alert('</p>')
//--></script>

...触发器:未打开的元素“P”的结束标记

<script type="text/javascript"><!--
alert('<\/p>')
//--></script>

...没有。

According to Mozilla:

For characters not listed [...] a preceding backslash is ignored, but this usage is deprecated and
should be avoided.

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:

var re = /^http:\/\//;

<2> It's required to avoid invalid HTML when you embed JavaScript code inside HTML:

<script type="text/javascript"><!--
alert('</p>')
//--></script>

... triggers: end tag for element "P" which is not open

<script type="text/javascript"><!--
alert('<\/p>')
//--></script>

... doesn't.

浮萍、无处依 2024-10-13 15:12:00

如果在作为转义序列没有意义的字符之前发现反斜杠,它将被忽略,即 "\/""/" 是相同的字符串JavaScript。

/ 字符是正则表达式分隔符,因此只需在正则表达式上下文中对其进行转义:

/[a-z]/[0-9]/   // Invalid.

/[a-z]\/[0-9]/  // Matches a lowercase letter, followed by a slash,
                // followed by a digit.

最后,如果要将反斜杠后跟的字符折叠到相应的转义序列中,您将必须替换整个表达式:

string expr = "name:\\t me";       // Backslash followed by `t`.
expr = expr.Replace("\\t", "\t");  // Tab character.

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:

/[a-z]/[0-9]/   // Invalid.

/[a-z]\/[0-9]/  // Matches a lowercase letter, followed by a slash,
                // followed by a digit.

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:

string expr = "name:\\t me";       // Backslash followed by `t`.
expr = expr.Replace("\\t", "\t");  // Tab character.
蓝眸 2024-10-13 15:12:00

如果 \ + 下一个字符不是转义序列,则 \ 被评估为 \。

例子:
\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

小傻瓜 2024-10-13 15:12:00

在 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 checking myString.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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文