diff --git a/tools/generation/lib/template.py b/tools/generation/lib/template.py
index fa891f00bef53eaa663578f936fe4f4df174ac66..66ef84071520d9726da0a38d96877c7e8d66a4e3 100644
--- a/tools/generation/lib/template.py
+++ b/tools/generation/lib/template.py
@@ -78,6 +78,13 @@ class Template:
         for region in self.regions:
             whitespace = indentPattern.match(lines[region['lineno']]).group(1)
             value = context['regions'].get(region['name'], '')
+
+            str_char = region.get('in_string')
+            if str_char:
+                safe_char = '"' if str_char == '\'' else '\''
+                value = value.replace(str_char, safe_char)
+                value = value.replace('\n', '\\\n')
+
             source = source[:region['firstchar']] + \
                 indent(value, whitespace).lstrip() + \
                 source[region['lastchar']:]
diff --git a/tools/generation/lib/util/find_comments.py b/tools/generation/lib/util/find_comments.py
index 81ad0ab55206fb6a858156bc36b641c32cd8cc35..bf4b0ffd7ba7c76301d1cc4791d0049f9b29754c 100644
--- a/tools/generation/lib/util/find_comments.py
+++ b/tools/generation/lib/util/find_comments.py
@@ -4,6 +4,7 @@
 def find_comments(source):
     '''Parse input string describing JavaScript source and yield dictionaries
     describing the JavaScript comments in the order they appear in the source.
+    This includes comment patterns within string literals.
 
     Each dictionary defines the following attributes:
 
@@ -11,6 +12,9 @@ def find_comments(source):
     - firstchar: the zero-indexed position of the token that begins the comment
     - lastchar: the zero-indexed position of the token that closes the comment
     - lineno: the zero-indexed offset of the line on which the comment appears
+    - in_string: `False` if the comment is a true JavaScript comment, one of
+                 '\'' (single quote), '"' (double quote), or '`' (back tick) if
+                 the comment pattern appears within a string literal.
     '''
     in_string = False
     in_s_comment = False
@@ -37,6 +41,7 @@ def find_comments(source):
                     source=comment[1:],
                     firstchar=idx - len(comment) - 1,
                     lastchar=idx,
+                    in_string=in_string,
                     lineno=lineno)
                 continue
         elif in_m_comment:
@@ -46,6 +51,7 @@ def find_comments(source):
                     source=comment[1:-1],
                     firstchar=idx - len(comment) - 1,
                     lastchar=idx + 1,
+                    in_string=in_string,
                     lineno=lineno)
                 continue
         elif in_string:
@@ -53,7 +59,6 @@ def find_comments(source):
                 in_string = False
             elif source[idx] == '\n' and in_string != '`' and not follows_escape:
                 in_string = False
-            continue
 
         if in_m_comment or in_s_comment:
             comment += source[idx]
diff --git a/tools/generation/test/expected/normal/path1-normal.js b/tools/generation/test/expected/normal/path1-normal.js
index 3bc10d0e9f283f5b882fc99207b812c3e75399a6..467f954725ddab2be0a9f01e8925a938e547dfb3 100644
--- a/tools/generation/test/expected/normal/path1-normal.js
+++ b/tools/generation/test/expected/normal/path1-normal.js
@@ -25,9 +25,10 @@ The following should not be expanded:
 */*{ first }*/
 //*{ first }*/
 // /*{ first }*/
-"/*{ first }*/"
-'/*{ first }*/'
+Quote characters: " ' `
+"Quote characters: ' ' `"
+'Quote characters: " " `'
 `
-/*{ first }*/`
+Quote characters: " ' '`
 
 'This is "teardown" code.';
diff --git a/tools/generation/test/fixtures/normal.case b/tools/generation/test/fixtures/normal.case
index 276b58ccddc87eab2b6e4c211a670f09fae54ee5..c97a6e7b7fc54cc2f069bcad94ceb31c69ef5093 100644
--- a/tools/generation/test/fixtures/normal.case
+++ b/tools/generation/test/fixtures/normal.case
@@ -23,5 +23,7 @@ First value
 Second value
 //- third
 Third value
+//- fourth
+Quote characters: " ' `
 //- teardown
 'This is "teardown" code.';
diff --git a/tools/generation/test/fixtures/normal/normal.template b/tools/generation/test/fixtures/normal/normal.template
index ac0bea1e159ce65e91b93b86d2921aed63effb4b..32bc9cff19f8c9f69fcc27427c32b9add851573e 100644
--- a/tools/generation/test/fixtures/normal/normal.template
+++ b/tools/generation/test/fixtures/normal/normal.template
@@ -21,7 +21,8 @@ The following should not be expanded:
 */*{ first }*/
 //*{ first }*/
 // /*{ first }*/
-"/*{ first }*/"
-'/*{ first }*/'
+/*{ fourth }*/
+"/*{ fourth }*/"
+'/*{ fourth }*/'
 `
-/*{ first }*/`
+/*{ fourth }*/`