我想在反引号里面写反引号。 (es6)
如何在反引号中创建反引号?
const apple = 'apple';
const grape = 'grape';
const money1 = 2000;
const money2 = 1000;
// before
console.log(`${apple} = ${grape === 'grape' ? '(' + money1 + ')' : '(' + money2 + ')'`;
// after <-- I want
console.log(`${apple} = ${grape === 'grape' ? \`(${money1})\` : \`(${money2})\`}`;
最佳答案
删除反斜杠:
const apple = 'apple';
const grape = 'grape';
const money1 = 2000;
const money2 = 1000;
console.log(`${apple} = ${grape === 'grape' ? `(${money1})` : `(${money2})`}`);
如果您要在模板字符串中打印文字背包,则通常只需使用反斜杠即可。
console.log(`\``);
关于javascript - 如何在反引号中创建反引号?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/56571925/