unterminated string literal

前端 未结 9 1343
轮回少年
轮回少年 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= "English Comprehension<\/strong>\
        \
        
      \
    • Synonyms/Antonyms/Word Meaning (Vocabulary)<\/li>\
    • Complete the Sentence (Grammar)<\/li>\
    • Spot error/Correct sentence (Grammar/sentence construction)<\/li>\
    • Sentence Ordering (Comprehension skills)<\/li>\
    • Questions based on passage (Comprehension skills)<\/li>\ <\/ul>\ ";

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

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

    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!
    

提交回复
热议问题