The string delimiter characters present problems if you need to include them inside a string. Typically you may want to include a single quote as an apostrophe, as in the contraction of do not to don't. Or you may want to enclose a spoken comment inside double quotation marks.
Because the single and double quotes are generally interchangeable in JavaScript, in most cases the problem is easy to solve.
To use " inside a string, you can do it like this:
myString = 'A man said "hello" to me';
Using a double quote to include single quotes in your string, you might do this:
myString = "Don't do that";
Using the backslash escape delimiter we can safely use any combination of quotes inside either sort. Like this:
myString = "A man said \"hello\" to me";
myString = 'Don\'t do that';
A further use for this is to escape a single dot character in regular expressions. This allows you to search for full stops in the source text because a dot in a Regular Expression is a wildcard character match. This technique is useful for other meta symbols in the regular expression set. Refer to the regular expression topic for more details.
Just to illustrate here is a simple example. This regular expression matches any single character at the start of a line:
/^./
This variant matches only full stops or periods at the start of the line:
/^\./
An escape sequence is a series of characters that taken together describe a character that cannot normally be represented in the code set that the source is written in or may be difficult to type on keyboards that are not specially designed for international character sets.
Escape Sequence | Name | Symbol |
---|---|---|
\" | Double Quote | " |
\' | Single Quote (Apostrophe) | ' |
\\ | Backslash | \ |
\a | Audible alert (MSIE displays the letter a) | <BEL> |
\b | Backspace (ignored silently in MSIE) | <BS> |
\f | Form Feed (ignored silently in MSIE) | <FF> |
\n | Line Feed (Newline - MSIE inserts a space) | <LF> |
\r | Carriage Return (MSIE inserts a space) | <CR> |
\t | Horizontal Tab (MSIE inserts a space) | <HT> |
\v | Vertical tab (MSIE displays the letter v) | <VT> |
\0nn | Octal escape | - |
\042 | Double Quote | " |
\047 | Single Quote (Apostrophe) | ' |
\134 | Backslash | \ |
\xnn | Hexadecimal escape | - |
\x22 | Double Quote | " |
\x27 | Single Quote (Apostrophe) | ' |
\x5C | Backslash | \ |
\unnnn | Unicode escape | - |
\u0022 | Double Quote | " |
\u0027 | Single Quote (Apostrophe) | ' |
\u005C | Backslash | \ |
\uFFFE | A special Unicode sentinel character for flagging byte reversed text | - |
\uFFFF | A special Unicode sentinel character | - |
Here are some examples of escape sequences for the apostrophe character.
In HTML apostrophe characters are escaped like this:
'
In C language characters are escaped like this for octal values:
\047
And like this for hexadecimal values:
\x27
And in JavaScript, Unicode characters can be escaped like this:
\u0027
Where the \u is followed by a four digit hexadecimal value.
ECMA 262 edition 2 - section - 2
ECMA 262 edition 2 - section - 6
ECMA 262 edition 2 - section - 7.7.3
ECMA 262 edition 3 - section - 2
ECMA 262 edition 3 - section - 6
ECMA 262 edition 3 - section - 7.7
O'Reilly JavaScript Definitive Guide - page - 38
Prev | Home | Next |
Error.toString() | Up | escape() |
JavaScript Programmer's Reference, Cliff Wootton Wrox Press (www.wrox.com) Join the Wrox JavaScript forum at p2p.wrox.com Please report problems to support@wrox.com © 2001 Wrox Press. All Rights Reserved. Terms and conditions. |