1.8 Using Special and Escaped Characters
NN 2, IE 3
1.8.1 Problem
You want
to add low-order ASCII characters (tab, carriage return, etc.) to a
string.
1.8.2 Solution
Use the escape sequences shown in Table 1-2 to
represent the desired character. For example, to include an
apostrophe inside a literal string, use \', as in:
var msg = "Welcome to Joe\'s Diner.";
1.8.3 Discussion
The core JavaScript language includes a feature common to most
programming languages that lets you designate special characters. A
special character is not one of the plain
alphanumeric characters or punctuation symbols, but has a particular
meaning with respect to whitespace in text. Common characters used
these days include the tab, newline, and carriage return.
A special character begins with a backslash, followed by the
character representing the code, such as \t for
tab and \n for newline. The backslash is called an
escape character, instructing the interpreter to
treat the next character as a special character. Table 1-2 shows the recognized escape sequence
characters and their meanings. To include these characters in a
string, include the backslash and special character inside the quoted
string:
var confirmString = "You did not enter a response to the last " +
"question.\n\nSubmit form anyway?";
If you want to use one of these symbols between variables that
contain string values, be sure the special character is quoted in the
concatenation statement:
var myStr = lineText1 + "\n" + lineText2;
Special characters can be used to influence formatting of text in
basic dialog boxes (from the alert( ),
confirm( ), and prompt( )
methods) and textarea form controls.
Table 1-2 shows the recognized escaped characters
and their meanings.
Table 1-2. String escape sequences
\b
|
Backspace
|
\t
|
Horizontal tab
|
\n
|
Line feed (newline)
|
\v
|
Vertical tab
|
\f
|
Form feed
|
\r
|
Carriage return
|
\"
|
Double quote
|
\'
|
Single quote
|
\\
|
Backslash
|
Note that to
include a visible backslash character in a string, you must use a
double backslash because a single one is treated as the invisible
escape character. Use the escaped quote symbols to include single or
double quotes inside a string.
While you can use an escaped character in tests for the existence of,
say, line feed characters in a string, you have to exercise some care
when doing so with the content of a
textarea element. The
problem accrues from a variety of implementations of how user-entered
carriage returns are coded in the
textarea's content. IE for
Windows inserts two escaped characters
(\r\n in
that sequence) whenever a user presses the Enter key to make a
newline in a textarea. But IE for Macintosh uses
only the \r character. And Netscape 6 and later
inserts \n for newlines. Navigator 4 is governed
more by the operating system in which the browser runs:
\r\n for Windows; \r for
Macintosh; and \n for Unix. This wide variety in
character combinations makes searches for user-typed line breaks
difficult to perform accurately across browsers and operating
systems.
Going the other way—creating a string for script insertion into
a textarea value—is easier because modern
browsers accommodate all symbols. Therefore, if you assign just
\r or \n or the combination
\r\n, all browsers interpret any one of them as a
carriage return, and convert the escape character(s) to match their
internal handling.
1.8.4 See Also
Recipe 1.1 for tips on concatenating strings—tips that apply
equally to escaped string characters.
|