How would you turn a JavaScript variable into a Template literal?

后端 未结 4 1348
走了就别回头了
走了就别回头了 2020-11-30 15:32

I would like to take text that I generated and stored in a string and use it like a template literal.

var generatedText = \"Pretend this text was generated          


        
4条回答
  •  [愿得一人]
    2020-11-30 15:59

    You should emulate a template literal instead, because letting text from ~somewhere~ run arbitrary JavaScript like a real template literal’s ${} sections can usually isn’t a good idea:

    generatedText.replace(/\$\{variable}/g, variable);
    

提交回复
热议问题