unterminated string literal

前端 未结 9 1307
轮回少年
轮回少年 2020-11-30 06:07

The following code:

var str= \"English Comprehension<\\/strong>
                    
                    
相关标签:
9条回答
  • 2020-11-30 06:32

    JavaScript doesn't allow literal line breaks in strings unless you escape them with \:

    var str= "<strong>English Comprehension<\/strong>\
        <br\/>\
        <ul>\
        <li>  Synonyms/Antonyms/Word Meaning (Vocabulary)<\/li>\
        <li>  Complete the Sentence (Grammar)<\/li>\
        <li>  Spot error/Correct sentence (Grammar/sentence construction)<\/li>\
        <li>  Sentence Ordering (Comprehension skills)<\/li>\
        <li>  Questions based on passage (Comprehension skills)<\/li>\
        <\/ul>\
        <br\/>";
    

    You might look at ES2015+ template literals instead, which use backticks instead of ' or " and allow literal line breaks:

    var str= `<strong>English Comprehension<\/strong>
        <br\/>
        <ul>
        <li>  Synonyms/Antonyms/Word Meaning (Vocabulary)<\/li>
        <li>  Complete the Sentence (Grammar)<\/li>
        <li>  Spot error/Correct sentence (Grammar/sentence construction)<\/li>
        <li>  Sentence Ordering (Comprehension skills)<\/li>
        <li>  Questions based on passage (Comprehension skills)<\/li>
        <\/ul>
        <br\/>`;
    

    But of course, that only works on ES2015-compatible JavaScript engines (or if you transpile).

    Note that within a template literal ${...} has special meaning (allowing you to substitute the result of any expression:

    let v = "Ma";
    console.log(`Look ${v}, no hands!`); // Look Ma, no hands!
    
    0 讨论(0)
  • 2020-11-30 06:34

    If you want to maintain your data in one block of string, try:

    var str= "<strong>English Comprehension</strong>\n\
                    <br />\n\
                    <ul>\n\
                    <li>  Synonyms/Antonyms/Word Meaning (Vocabulary)</li>\n\
                    <li>  Complete the Sentence (Grammar)</li>\n\
                    <li>  Spot error/Correct sentence (Grammar/sentence construction)</li>\n\
                    <li>  Sentence Ordering (Comprehension skills)</li>\n\
                    <li>  Questions based on passage (Comprehension skills)</li>\n\
                    </ul>\n\
                    <br />";
    

    notice the \n\ at the end of each line.

    0 讨论(0)
  • 2020-11-30 06:36

    Remove the newlines, it will work. GOTCHA to keep in mind ;) See it simple works all fine here without newlines: http://jsfiddle.net/87dYh/

    var str= "<strong>English Comprehension<\/strong><br\/><ul>  <li>  Synonyms/Antonyms/Word Meaning (Vocabulary)<\/li>                  <li>  Complete the Sentence (Grammar)<\/li>                   <li>  Spot error/Correct sentence (Grammar/sentence construction)<\/li>     <li>  Sentence Ordering (Comprehension skills)<\/li>                  <li>  Questions based on passage (Comprehension skills)<\/li>                 <\/ul>              <br\/>";
    
    alert(str);
    
    0 讨论(0)
提交回复
热议问题