呈现多个Javascript变量的多个模板文本

呈现多个Javascript变量的多个模板文本,javascript,str-replace,template-literals,Javascript,Str Replace,Template Literals,我上面有以下代码,来自: 这完全适用于一个变量,但不适用于多个变量,这是我的意图,如: const interpolate=(str,obj)=>str.replace( /\${([^}]+)}/g, (u,prop)=>obj[prop] ); const generatedText=“你好${Name}!你妈妈${Mother}在叫你!”; const Name=“Ana”; const Mother=“麦莉”; log(插入(generatedText,{Name},{Mother})

我上面有以下代码,来自:

这完全适用于一个变量,但不适用于多个变量,这是我的意图,如:

const interpolate=(str,obj)=>str.replace(
/\${([^}]+)}/g,
(u,prop)=>obj[prop]
);
const generatedText=“你好${Name}!你妈妈${Mother}在叫你!”;
const Name=“Ana”;
const Mother=“麦莉”;
log(插入(generatedText,{Name},{Mother}));

//应该打印:你好,安娜!你妈妈麦莉在叫你
将传递给
interpolate
的第二个参数设置为具有多个属性的单个对象。每当您希望能够插入其他内容时,请向对象添加属性(并将
${…}
添加到
生成的文本
字符串):

const interpolate=(str,obj)=>str.replace(
/\${([^}]+)}/g,
(u,prop)=>obj[prop]
);
const generatedText=“你好${Name}!你妈妈${Mother}在叫你!”;
const Name=“Ana”;
const Mother=“麦莉”;
log(插入(generatedText,{Name,Mother}));
//应该打印:你好,安娜!你妈妈麦莉在叫你
const interpolate = (str, obj) => str.replace(
  /\${([^}]+)}/g,
  (_, prop) => obj[prop]
);

const generatedText = "But I still need to use it as a template it to get ${variable}.";
const variable = "Successs!!!!";

console.log(interpolate(generatedText, { variable }));