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.
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.
|\'||Single Quote (Apostrophe)||'|
|\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>|
|\047||Single Quote (Apostrophe)||'|
|\x27||Single Quote (Apostrophe)||'|
|\u0027||Single Quote (Apostrophe)||'|
|\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:
And like this for hexadecimal values:
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