-1

What is the easiest / most concise way to inject values into a string without using a RegEx expression or complex function?

For example, this:

var a = 'cats';
var b = 'dogs';

var result = String.format('%a and %b living together', a, b);
console.log(result);

... should yield ...

cats and dogs living together

I'm coming from the C# world where this is ridiculously easy. Every search on SO turns up some type of overcomplicated RegEx expression or replace function.

G. Deward
  • 1,542
  • 3
  • 17
  • 30

1 Answers1

3

Template literals is probably the most concise (require ES6).

var result = `${a} and ${b} living together`;

On the other hand, regular string concatenation isn't that bad either.

var result = '' + a + ' and ' + b + ' living together';
Gilles Quénot
  • 173,512
  • 41
  • 224
  • 223
Joseph
  • 117,725
  • 30
  • 181
  • 234