Template literals with nested backticks(`) in ES6

前端 未结 8 994
离开以前
离开以前 2020-12-03 04:23

How can I write a template literal in ECMAScript 6 that will contain backticks(`) in and by itself, (i.e. nested backticks)?

For example:

var qu         


        
8条回答
  •  一个人的身影
    2020-12-03 05:08

    As mentioned in other answers, you can escape the backtick ` with a backslash, like \`.

    var tripleBacktickExample = `
    \`\`\`python
    # This JavaScript string contains some example Markdown that
    # uses triple-backticks to delimit a Python code block.
    \`\`\`
    `
    

    However, if you need very many backticks in a row ````` inside the template literal, it could be more readable to put them within a normal string that is inside a placeholder, like ${'`````'} or ${"`````"}.

    var tripleBacktickExample = `
    ${'```'}python
    # This JavaScript string contains some example Markdown that
    # uses triple-backticks to delimit a Python code block.
    ${'```'}
    `
    

提交回复
热议问题