Nested quotation

A nested quotation is a quotation that is encapsulated inside another quotation, forming a hierarchy with multiple levels. When focusing on a certain quotation, one must interpret it within its scope. Nested quotation can be used in literature (as in nested narration), speech, and computer science (as in "meta"-statements that refer to other statements as strings). Nested quotation can be very confusing until evaluated carefully and until each quotation level is put into perspective.

In literature

Normally, hierarchical quotation sublevels alternate between using the quotation marks ‘…’ (single quotes) and “…” (double quotes). It is a matter of stylistic preference whether single or double quotation marks first, so both of the following examples are valid:

He said, “She yelled, ‘I’m going to kill you!’ ”
He said, ‘She yelled, “I’m going to kill you!” ’

However, American style strongly favors double quotes for the top level, while British style strongly favors single quotes for the top level. Consider this example from Heart of Darkness by Joseph Conrad, a novel that uses nested quotes throughout the entirety of its narration, telling the story from the first-person perspective of the narrator, Marlow:

[Marlow said,] “…The fat man sighed. ‘Very sad.’ ‘And the pestiferous absurdity of his talk,’ continued the other; ‘he bothered me enough when he was here. “Each station should be like a beacon on the road towards better things, a center for trade of course, but also for humanizing, improving, instructing.” Conceive you—that ass! And he wants to be manager! No, it’s—’ Here he got choked by excessive indignation, and I lifted my head the least bit. …”

In the above example, Marlow tells a story about a fat man sighing and another person talking about someone who says bothersome things. The latter person Marlow quotes, in turn, quotes the person he is talking about, leading to a second-level nested quote.

In JavaScript programming

Nested quotes often become an issue using the eval keyword. The eval function is a function that converts and interprets a string as actual JavaScript code, and runs that code. If that string is specified as a literal, then the code must be written as a quote itself (and escaped accordingly).

For example:

eval("var a=3; alert();");

This code declares a variable a, which is assigned the value 3, and a blank alert window is popped up to the user.

Nested strings (level 2)

Suppose we had to make a quote inside the quoted interpreted code. In JavaScript, you can only have one unescaped quote sublevel, which has to be the alternate of the top-level quote. If the 2nd-level quote symbol is the same as the first-level symbol, these quotes must be escaped. For example:

alert("I don't need to escape here");
alert('Nor is it "required" here');
alert('But now I do or it won\'t work');

Nested strings (level 3 and beyond)

Furthermore (unlike in the literature example), the third-level nested quote must be escaped in order not to conflict with either the first- or second-level quote delimiters. This is true regardless of alternating-symbol encapsulation. Every level after the third level must be recursively escaped for all the levels of quotes in which it is contained. This includes the escape character itself, the backslash (“\”), which is escaped by itself (“\\”).

For every sublevel in which a backslash is contained, it must be escaped for the level above it, and then all the backslashes used to escape that backslash as well as the original backslash, must be escaped, and so on and so forth for every level that is ascended. This is to avoid ambiguity and confusion in escaping.

Here are some examples that demonstrate some of the above principles:

document.write("<html><head></head><body><p>Hello, this is the body of the document.");
document.writeln("</p>");
document.write("<p>A newline in HTML code 
acts simply as whitespace, whereas a <br> starts a new line.");
document.write("</p></body></html>\n"); 

eval('eval(\"eval(\\\"alert(\\\\\\\"Now I\\\\\\\\\\\\\\\'m confused!\\\\\\\")\\\")\")');

Note that the number of backslashes increase from 0 to 1 to 3 to 7 to 15, indicating a 2^n-1 rule for successively nested symbols.

See also

This article is issued from Wikipedia - version of the Sunday, October 25, 2015. The text is available under the Creative Commons Attribution/Share Alike but additional terms may apply for the media files.