0

Given this?

var v = 10;
var template = "testing ${v}...";

How do I achieve the same result as the following template string.

var result = `testing ${v}...`;

The template string is stored in a variable, and not a literal template string.

Is there a method to apply a template string when the template string is stored in a variable, and not literal?

Jim
  • 14,952
  • 15
  • 80
  • 167

1 Answers1

0

var v = 10;
var template = "testing ${v}...";

var output = template.replace("${v}", `${v}`);

alert(output);
void
  • 36,090
  • 8
  • 62
  • 107
  • I need the template string to be opaque. If I have to search and replace each possible variable, I might as well not use the template string, and just do a regular search and replace. – Jim Dec 31 '15 at 04:54
  • @jim - Javascript doesn't have automatic string interpolation. The closest you'll be able to get is manual interpolation with `eval`: `var a = 1; eval('var b = "testing '+a+'...";'); console.log(b);` – Jared Farrish Dec 31 '15 at 13:02