简体   繁体   English

\\对非转义字符做什么?

[英]what does \ do on non escape characters?

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. 根据http://www.c-point.com/javascript_tutorial/special_characters.htm,其中有一些转义字符,例如\\ n和\\ 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' . 我在javascript'http '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 另一个问题是,如果我有name:\\t me (或"name:\\\\t me"是否有逃逸它的函数,所以有一个制表符?我正在使用C#,这些字符串来自JSON文件

According to Mozilla: 根据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 https://developer.mozilla.org/zh-CN/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: <1>需要在使用/foo/语法的正则表达式中转义文字斜杠:

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

<2> It's required to avoid invalid HTML when you embed JavaScript code inside HTML: <2>将JavaScript代码嵌入HTML时,必须避免使用无效的HTML:

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

... triggers: end tag for element "P" which is not open ...触发器: 元素“ P”的结束标签未打开

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

... doesn't. ...没有。

If a backslash is found before a character which is not meaningful as an escape sequence, it will be ignored, ie "\\/" and "/" are the same string in Javascript. 如果在一个无意义的转义字符之前找到反斜杠,则它将被忽略,即"\\/""/"在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.

\\ is evaluated as \\ if \\ + next character is not an escape sequence. 如果\\ +下一个字符不是转义序列,则将\\评估为\\。

examples: 例子:
\\t -> escape sequence t -> tab \\ t->转义序列t->选项卡
\\\\t -> escape \\ and t -> \\t \\\\ t->转义\\和t-> \\ t
\\\\ -> escape sequence \\ -> \\ \\\\->转义序列\\-> \\
\\c -> \\c (not an escape sequence) \\ c-> \\ c(不是转义序列)
\\a -> escape sequence a -> ??? \\ a->转义序列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. 实际上,它甚至更不标准:在基本C'\\ y'-> y +警告中,而不是\\ y。 So this is very language dependent, be careful. 因此,这非常依赖于语言,请小心。 (disregard my comment below). (无视下面我的评论)。

br, br

Juha 朱哈

edit: What language are you using?= Java and c have slightly different behavior. 编辑:您使用什么语言?= Java和c的行为略有不同。

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 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.cerritos.edu/jwilson/cis_182/language_resources/java_escape_sequences.htm
http://www.java2s.com/Code/Python/String/EscapeCodesbtnar.htm http://www.java2s.com/Code/Python/String/EscapeCodesbtnar.htm

In C# you can use the backslash character to tell the compiler what you really want. 在C#中,您可以使用反斜杠字符来告诉编译器您真正想要的是什么。 After compiling though, these escape characters do not exist. 但是,编译后,这些转义字符不存在。

If you use string myString = "\\t"; 如果使用string myString = "\\t"; the string will actually contain a TAB character, not just represent one. 该字符串实际上将包含一个TAB字符,而不仅仅是代表一个。 You can test this by checking myString.Length which is 1. 您可以通过检查为1的myString.Length进行测试。

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: 但是,如果要将字符“反斜杠”和“ t”发送到JSON客户端,则必须告诉编译器通过转义反斜杠来避免反斜杠:

string myString = "\\\\t"; will result in a string of two characters, the "backslash" and the "t". 将产生两个字符的字符串,即“反斜杠”和“ 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. 如果您必须跨越多个转义和转义的层次,事情会变得混乱,请尝试调试这些层次以了解幕后的真实情况。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM