admin管理员组文章数量:1417661
var a = "Test \'"
> undefined
var b = "Test '"
> undefined
a === b
> true
a
> "Test '"
Yep. I know that both stings are equal ( a show it in example code ) - the question is why
Why escaping sing quote produce such strange result? Explain please.
Thx in advance
var a = "Test \'"
> undefined
var b = "Test '"
> undefined
a === b
> true
a
> "Test '"
Yep. I know that both stings are equal ( a show it in example code ) - the question is why
Why escaping sing quote produce such strange result? Explain please.
Thx in advance
Share Improve this question edited May 4, 2016 at 13:13 Vasiliy vvscode Vanchuk asked May 4, 2016 at 13:08 Vasiliy vvscode VanchukVasiliy vvscode Vanchuk 7,1892 gold badges24 silver badges46 bronze badges 05 Answers
Reset to default 3I know that both stings are equal ( a show it in example code ) - the question is why
The escape character is never part of the string value. It's only used by the parser to determine the string value.
The spec explains how escape sequences are evaluated:
You can see that escape sequences in double quoted strings (DoubleStringCharacter
) is evaluated exactly the same as escape sequences in single quoted strings (SingleStringCharacter
). Both evaluate \ EscapeSequence
.
The backslash in the string is used to escape the following character. When using single quote inside a double-quoted string, the single quote is not needed to be escaped.
Thus,
"Test \'s" === "Test 's"
'Test\"s' === 'Test"s'
Consider a case where you want to include both single quotes and double quotes in string. In this case, there should be some way to escape the inner quotes otherwise piler will throw an error at invalid string. By using backslash \
, the following character can be escaped and it'll be considered as normal literal instead of it's special meaning in the context.
"He said, \"My name is something, I stay at my uncle's house.\""
They are equal:
"Test '".length // 6
"Test \'".length // 6
The following table lists the special characters that you can use in JavaScript strings: \0 \b \f \n \r \t \v \' \" \\ \XXX \xXX \uXXXX \u{XXXXX}
For characters not listed in the table, a preceding backslash is ignored, but this usage is deprecated and should be avoided.
Resume:
JavaScript converts the string "Test \'"
to "Test '"
and then pares with the other "Test '"
string.
It looks to me like the interpreter is automatically escaping out the single quote ' due to the presence of the double quote " So it sees the string starts with " and accepts anything in the text.
It expects \ to be an escape character so \' will produce just ' It also expects " to terminate the string so will allow ' in the string body just the same. Thus in memory, both strings will be
Test "
This is just my guess, but it seems the most reasonable to me. a===b is true because both strings are actually the same. I don't know how javascript works in the background but in most programming languages, a and b would actually both be pointing to the same object in memory.
Your escaping is useless, so the interpretor ignore it.
var a = "Test \'"
var b = "Test '"
a
> "Test '"
b
> "Test '"
if you write:
var a = "\a";
a
> "a"
As i said, un unexpected excaping is just ignored.
本文标签: javascriptStrange single quote escapingStack Overflow
版权声明:本文标题:javascript - Strange single quote escaping - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745278287a2651303.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论