为此,请replace()在JavaScript中使用。代码如下-
以下是代码-
var temp = `My name is {{fullName}} I live in {{countryName}}`;
var details = {
"fullName": "David Miller",
"countryName": "AUS"
}
replaceName = temp.replace(
/\{\{(.+?)\}\}/g,
(matching, value) => details[value.trim()]
);
console.log(replaceName);要运行以上程序,您需要使用以下命令-
node fileName.js.
在这里,我的文件名为demo256.js。
输出结果
这将在控制台上产生以下输出-
PS C:\Users\Amit\javascript-code> node demo256.js My name is David Miller I live in AUS