When we write JavaScript scripts, we may want HTML documents to display or use certain special characters (such as quotes or slashes). (For example: ) but as mentioned earlier, when declaring a string, it must be enclosed in quotation marks. As a result, the quotation marks in the string may be confused with the quotation marks that mark the string, and the escape character (Escape Character) is used at this point.
JavaScript uses the following eight escape characters. These characters are all started with a backslash (\). When the JavaScript interpreter (interpreter) sees the backslash, it takes special care to show what the programmer wants to say.
The following table lists the escape sequences for JavaScript and the characters they represent. Of these, two escape sequences are generic and can represent arbitrary characters by representing Latin-1 or Unicode character encodings as hexadecimal numbers. For example, the escape sequence \xa9 represents a copyright symbol that uses a hexadecimal number A9 to represent the Latin-1 encoding. Similarly, \u represents any Unicode character specified by a four-bit hexadecimal number, such as \U03C0, which represents the character Pi (pi). Note that although the ECMAScript V1 standard requires Unicode character escape, the version prior to JavaScript 1.3 usually does not support escape characters. Some JavaScript versions also allow a backslash to be followed by a three-digit octal number to represent the Latin-1 character, but the ECMAScript V3 standard does not support this escape sequence, so they should not be used again.
|
Preface |
Escape Character |
Instructions for use |
0 |
|
NUL character (\u0000) |
1 |
\b |
Back one lattice (Backspace) backspace (\u0008) |
2 |
\f |
Change page (Form Feed) (\u000c) |
3 |
\ n |
NewLine (New line) (\u000a) |
4 |
\ r |
Enter (carriage return) (\u000d) |
5 |
\ t |
tab (tab) Horizontal tab (\u0009) |
6 |
\' |
Single quotation mark (\u0027) |
7 |
\" |
Double quotation mark (\u0022) |
8 |
\\ |
Backslash (backslash) (\u005c) |
9 |
\v |
Vertical tab (\U000B) |
10 |
\xnn |
The Latin-1 character specified by two-bit hexadecimal value nn |
11 |
\unnnnn |
Unicode characters specified by the four-bit hexadecimal number nnnn |
12 |
\nnn |
A Latin-1 character specified by one to three-bit octal (1 to 377). ECMAScript v3 not supported, do not use this escape sequence |
\o nul character (\u0000) \b Backspace (\u0008) \ t Horizontal tab (\U0009) \ n line feed (\u000a) \v vertical tab (\U000B) \f Page Feed (\u000c) \ r return character (\u000d) \ double quotation mark (\u0022) \ ' apostrophe or single quotation mark (\u0027) \ Reverse slash (\u005c) \xxx the Latin-1 character specified by the two-digit hexadecimal number XX \uxxxx Unicode characters specified by the 4-bit hexadecimal number xxxx
The ASC for the space is 32, converted to 16 is x20, using the escape character for \x20 Here's a nice table.
|
Unicode Character Value |
Escape sequence |
meaning |
category |
\u0008 |
\b |
Backspace |
|
\u0009 |
\ t |
Tab |
Blank |
\u000a |
\ n |
Line Break (linefeed) |
Line Terminator |
\u000b |
\v |
Vertical tab |
Blank |
\u000c |
\f |
Change page |
Blank |
\u000d |
\ r |
Enter |
Line Terminator |
\u0020 |
|
Space |
Blank |
\u0022 |
\" |
Double quotation mark (") |
|
\u0027 |
\' |
Single quotation mark (') |
|
\u005c |
\\ |
Back slash (\) |
|
\u00a0 |
|
nonbreaking spaces |
Blank |
\u2028 |
|
Row separator |
Line Terminator |
\u2029 |
|
Paragraph separator |
Line Terminator |
\ufeff |
|
BYTE order mark |
Blank
|
|
Example: <script Language = "JAVAScript" > <!-- Use (\) to indicate (") document.write ("Our Lover \"); document.write ("Use (\) to indicate (\) document.write ("Documents under C:\\windows\\"); document.write ("Use (\ n) to indicate line wrapping Alert ("MM is a Kiss"); document.write ("Use (\ n) to indicate line wrapping document.write ("<pre> mm is a kiss </pre>"); document.write ("--> </Script>
|
|
|
Description: 1, "\ n" This escape character is often used with alert (), which is equivalent to pressing the "Enter" key in the text editor (in VBScript, the constant "vbCrLf"). 2, if you must use "\ n" in document.write (), you must match the HTML <PRE> tag. (General use <br>). |