开发者

Why document.write('\45'); let me get %?

I've code below:

<!DOCTYPE html>
<meta charset="UTF-8">
<title>An HTML5 document</title>
<script>
    document.write('\45');
</script>

The result is that I get a '%'. Why? I 开发者_JAVA百科can't understand the code.

Thank you!


The character % has the ASCII value 37.

\45 is an octal escape code for character 37 (because 4 * 8 + 5 = 37).

Note that according to the Mozilla Developer Network the usage of octal escape codes in string literals is deprecated:

Octal escape sequences (\ followed by one, two, or three octal digits) are deprecated in string and regular expression literals.


The backslash (\) is used as an escape character. \45 refers to the ASCII Code 45, which is %. If you want your code to output \45, you have to escape the backslash by writing

document.write('\\45');


% is ASCII code 45 - http://www.robelle.com/smugbook/ascii.html So \45 means "Write out ASCII character code 45".

If you want the literal string \45 writing out, you need to escape the \ character with another \

<script>
        document.write('\\45');
</script>


\45 is the character value. Like if you would look directly into your memory, the character "%" would be stored as the value 45

Which character is which is determined by the character encoding, like ASCII and UTF-8

Things like A and a also have different values. A is probably the value of a - 26 but not sure there though..


These are Octal values for these values: http://www.w3schools.com/TAGS/ref_ascii.asp

increment or decrement your number, you will get ampersand and single quote. http://jsfiddle.net/wrd8X/

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜