How to interpolate variables in strings in JavaScript, without concatenation?

前端 未结 16 2245
长情又很酷
长情又很酷 2020-11-22 02:41

I know in PHP we can do something like this:

$hello = \"foo\";
$my_string = \"I pity the $hello\";

Output: \"I pity the foo\"<

16条回答
  •  不思量自难忘°
    2020-11-22 02:58

    Prior to Firefox 34 / Chrome 41 / Safari 9 / Microsoft Edge, no. Although you could try sprintf for JavaScript to get halfway there:

    var hello = "foo";
    var my_string = sprintf("I pity the %s", hello);
    

提交回复
热议问题