小编Cha*_*era的帖子

用javascript对象替换字符串值

我目前正在为NodeJs制作一个小模块.我需要一点帮助.

我会这样说的.我有一个带字符串的变量.它包含一个字符串html值.现在我需要$(title)用我的对象替换这样的东西{ "title" : "my title" }.这可以扩展到用户提供的任何内容.这是当前的代码.我认为我需要RegEx才能做到这一点.你能帮助我吗?

var html = `<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Document $(title)</title>
</head>
<body>

  <h1>Test file, $(text)</h1>

</body>
</html>`;

function replacer(html, replace) {
  // i need a regex to replace these data
  //return replacedData;
}

replacer(html, { "title" : "my title", "text" : "text is this" });
Run Code Online (Sandbox Code Playgroud)

javascript regex template-engine object node.js

5
推荐指数
2
解决办法
9939
查看次数

标签 统计

javascript ×1

node.js ×1

object ×1

regex ×1

template-engine ×1