Nested quote
Encyclopedia
A nested quotation is a quotation
Quotation
A quotation or quote is the repetition of one expression as part of another one, particularly when the quoted expression is well-known or explicitly attributed by citation to its original source, and it is indicated by quotation marks.A quotation can also refer to the repeated use of units of any...

 that is encapsulated
Encapsulation
- Chemistry :* Molecular encapsulation, in chemistry, the confinement of an individual molecule within a larger molecule* Capsule , in pharmacy, the enclosure of a medicine within a relatively stable shell for administration...

 inside another quotation, forming a hierarchy
Hierarchy
A hierarchy is an arrangement of items in which the items are represented as being "above," "below," or "at the same level as" one another...

 with multiple levels. When focusing on a certain quotation, one must interpret it within its scope
Scope
The word scope may refer to many different devices or viewing instruments, constructed for many different purposes. It may refer to a telescopic sight, an optical device commonly used on firearms. Other uses of scope or Scopes may refer to:...

. 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
String (computer science)
In formal languages, which are used in mathematical logic and theoretical computer science, a string is a finite sequence of symbols that are chosen from a set or alphabet....

). Nested quotation can be very confusing until evaluated carefully and until each quotation level is put into perspective.

Convention

Normally, hierarchical quotation sublevels alternate between using the quotation delimiter
Delimiter
A delimiter is a sequence of one or more characters used to specify the boundary between separate, independent regions in plain text or other data streams. An example of a delimiter is the comma character, which acts as a field delimiter in a sequence of comma-separated values.Delimiters represent...

  (double quote) and ' (single quote). This is normally true, regardless of the quotation style—whether the start and end quote symbols are uniform (straight quotes) or different (smart quotes, with the quote symbols at the beginning and end pointing inwards in their respective directions towards the quote text). For straight quotes (normally used in plaintext), the alternation between single and double quotes is necessary. For smart quotes, it is not as necessary, since the directional quote symbols behave analogously to nested parentheses, since the delimiters are unique and it is clear where a quote opens (beginning a new sublevel) and ends (ending that sublevel and returning to the outer scope). However, convention dictates that alternating symbols are used either way.

Example in literature

In literature, a good example of a story that uses nested quotes throughout the entirety of its narration, is Heart of Darkness
Heart of Darkness
Heart of Darkness is a novella written by Joseph Conrad. Before its 1903 publication, it appeared as a three-part series in Blackwood's Magazine. It was classified by the Modern Library website editors as one of the "100 best novels" and part of the Western canon.The story centres on Charles...

 by Joseph Conrad
Joseph Conrad
Joseph Conrad was a Polish-born English novelist.Conrad is regarded as one of the great novelists in English, although he did not speak the language fluently until he was in his twenties...

. It can be very confusing to English classes because of its complex hierarchical structure of the narrator narrating in the first person, giving the majority of the story to dialog from one character (Marlow) who speaks in the first person, who quotes many other characters, and often quoting those characters' quotes of other characters.

Consider this example from that story (page 29):
[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. ..."

Marlow is telling the 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 2nd-level nested quote.

Example in JavaScript programming

Nested quotes often become an issue using the eval keyword. The eval function
Eval
In some programming languages, eval is a function which evaluates a string as though it were an expression and returns a result; in others, it executes multiple lines of code as though they had been included instead of the line including the eval...

 is a function that converts and interprets a string
String (computer science)
In formal languages, which are used in mathematical logic and theoretical computer science, a string is a finite sequence of symbols that are chosen from a set or alphabet....

 as actual JavaScript
JavaScript
JavaScript is a prototype-based scripting language that is dynamic, weakly typed and has first-class functions. It is a multi-paradigm language, supporting object-oriented, imperative, and functional programming styles....

 code, and runs that code. If that string is specified as a literal
String literal
A string literal is the representation of a string value within the source code of a computer program. There are numerous alternate notations for specifying string literals, and the exact notation depends on the individual programming language in question...

, then the code must be written as a quote itself (and escaped
Escape character
In computing and telecommunication, an escape character is a character which invokes an alternative interpretation on subsequent characters in a character sequence. An escape character is a particular case of metacharacters...

 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
Recursion
Recursion is the process of repeating items in a self-similar way. For instance, when the surfaces of two mirrors are exactly parallel with each other the nested images that occur are a form of infinite recursion. The term has a variety of meanings specific to a variety of disciplines ranging from...

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("

Hello, this is the body of the document.");
document.writeln("

");
document.write("

A newline in HTML code
acts simply as whitespace, whereas a <br> starts a new line.");
document.write("

\n");

eval('eval(\"eval(\\\"alert(\\\\\\\"Now I\\\\\\\\\\\\\\\'m confused!\\\\\\\")\\\")\")');
The source of this article is wikipedia, the free encyclopedia.  The text of this article is licensed under the GFDL.
 
x
OK