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 0
Add a ment  | 

5 Answers 5

Reset to default 3

I 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