代码之家  ›  专栏  ›  技术社区  ›  Jake T.

如何将一对反勾号添加到由javascript字符串插值生成的字符串中?

  •  0
  • Jake T.  · 技术社区  · 7 年前

    我正在使用字符串内插来创建一个我要发送给另一个服务的字符串。另一项服务允许在显示文本时进行反向标记以格式化文本。我希望在这个字符串中包含反勾号,以便最终用户更容易阅读。

    我发现 是的,但看起来很马虎。我需要6个字符才能将1个字符添加到输出中,并将我的小拇指发送到整个键盘上,这使得输入困难:

    const myString = `There will be one here -> ${'`'} and one here -> ${'`'}.`;
    // There will be one here -> ` and one here -> `.
    

    我有更好的办法吗?

    1 回复  |  直到 7 年前
        1
  •  3
  •   Jason Cust    7 年前

    MDN

    const myString = `There will be one here -> ${'`'} and one here -> ${'`'}.`;
    const myString2 = `There will be one here -> \` and one here -> \`.`;
    
    console.log(myString === myString2);
    推荐文章