Javascript 替换文件循环中的多个值
我正在尝试为自己构建一个快速而肮脏的静态站点生成器 假设我有一个Javascript 替换文件循环中的多个值,javascript,node.js,Javascript,Node.js,我正在尝试为自己构建一个快速而肮脏的静态站点生成器 假设我有一个test.html文件: {title} {downloadpath} 这是我的current.json,在这里我得到了要替换的值: { "id": 123, "album" : [{ "title": "Test EP", "albumid": 1234, "path": "test.zip" }] } 我的替换函数如下所示: // Iterat
test.html
文件:
{title}
{downloadpath}
这是我的current.json
,在这里我得到了要替换的值:
{
"id": 123,
"album" : [{
"title": "Test EP",
"albumid": 1234,
"path": "test.zip"
}]
}
我的替换函数如下所示:
// Iterate through JSON object and replace
function iterate(obj) {
for (var property in obj) {
if (obj.hasOwnProperty(property)) {
if (typeof obj[property] == "object")
iterate(obj[property]);
else
console.log("replace {" + property + "} with " + obj[property] )
htmldata.replace(/\{property\}/g, obj[property]);
}
}
}
iterate(json)
var result = htmldata
console.log(result)
// Write new HTML
fs.writeFile("test-" + json.id + ".html", result, 'utf8', function (err) {
if (err) {
return console.log(err);
}
});
replace {id} with 123
replace {title} with Test EP
replace {albumid} with 1234
replace {path} with test.zip
{title}
{path}
如果我运行它,它的工作原理如下:
// Iterate through JSON object and replace
function iterate(obj) {
for (var property in obj) {
if (obj.hasOwnProperty(property)) {
if (typeof obj[property] == "object")
iterate(obj[property]);
else
console.log("replace {" + property + "} with " + obj[property] )
htmldata.replace(/\{property\}/g, obj[property]);
}
}
}
iterate(json)
var result = htmldata
console.log(result)
// Write new HTML
fs.writeFile("test-" + json.id + ".html", result, 'utf8', function (err) {
if (err) {
return console.log(err);
}
});
replace {id} with 123
replace {title} with Test EP
replace {albumid} with 1234
replace {path} with test.zip
{title}
{path}
你可以看到问题就在那里。我认为它总是用输入文件替换编辑过的文件,所以我看不到任何更改。我想不出来,如果有人能给我指出正确的方向,我将不胜感激
谢谢 在
if
语句周围不使用大括号将导致微妙的错误
你想要:
if (typeof obj[property] == "object") {
iterate(obj[property]);
} else {
console.log("replace {" + property + "} with " + obj[property] )
htmldata.replace(/\{property\}/g, obj[property]);
}
否则,无论if
上的条件如何,每次都将运行替换
第二件事:正则表达式尝试匹配文本字符串“{property}”
。相反,请尝试以下方法:
htmldata.replace(new RegExp("{" + property + "}", "g"), obj[property]);
第三件事:您没有将replace
的结果分配回htmldata
。因此,您需要这样做:
htmldata = htmldata.replace(new RegExp("{" + property + "}", "g"), obj[property]);
在if
语句周围不使用大括号将导致微妙的错误
你想要:
if (typeof obj[property] == "object") {
iterate(obj[property]);
} else {
console.log("replace {" + property + "} with " + obj[property] )
htmldata.replace(/\{property\}/g, obj[property]);
}
否则,无论if
上的条件如何,每次都将运行替换
第二件事:正则表达式尝试匹配文本字符串“{property}”
。相反,请尝试以下方法:
htmldata.replace(new RegExp("{" + property + "}", "g"), obj[property]);
第三件事:您没有将replace
的结果分配回htmldata
。因此,您需要这样做:
htmldata = htmldata.replace(new RegExp("{" + property + "}", "g"), obj[property]);
在if
语句周围不使用大括号将导致微妙的错误
你想要:
if (typeof obj[property] == "object") {
iterate(obj[property]);
} else {
console.log("replace {" + property + "} with " + obj[property] )
htmldata.replace(/\{property\}/g, obj[property]);
}
否则,无论if
上的条件如何,每次都将运行替换
第二件事:正则表达式尝试匹配文本字符串“{property}”
。相反,请尝试以下方法:
htmldata.replace(new RegExp("{" + property + "}", "g"), obj[property]);
第三件事:您没有将replace
的结果分配回htmldata
。因此,您需要这样做:
htmldata = htmldata.replace(new RegExp("{" + property + "}", "g"), obj[property]);
在if
语句周围不使用大括号将导致微妙的错误
你想要:
if (typeof obj[property] == "object") {
iterate(obj[property]);
} else {
console.log("replace {" + property + "} with " + obj[property] )
htmldata.replace(/\{property\}/g, obj[property]);
}
否则,无论if
上的条件如何,每次都将运行替换
第二件事:正则表达式尝试匹配文本字符串“{property}”
。相反,请尝试以下方法:
htmldata.replace(new RegExp("{" + property + "}", "g"), obj[property]);
第三件事:您没有将replace
的结果分配回htmldata
。因此,您需要这样做:
htmldata = htmldata.replace(new RegExp("{" + property + "}", "g"), obj[property]);
您可以简单地使用下划线的.template
功能和跳过自动滚动模板功能您可以简单地使用下划线的.template
功能和跳过自动滚动模板功能您可以简单地使用下划线的.template
功能和跳过自动滚动模板功能您只需使用下划线的\u.template
功能和跳过自动滚动模板功能htmldata.replace()
的结果也需要重新分配?@pero啊,是的,需要。我应该把整行写下来——我只写了替换部分。现在编辑!谢谢!它现在工作得很好,我实际上已经看了几个小时的代码了。htmldata.replace()
的结果也需要重新分配吗?@pero啊,是的,需要。我应该把整行写下来——我只写了替换部分。现在编辑!谢谢!它现在工作得很好,我实际上已经看了几个小时的代码了。htmldata.replace()
的结果也需要重新分配吗?@pero啊,是的,需要。我应该把整行写下来——我只写了替换部分。现在编辑!谢谢!它现在工作得很好,我实际上已经看了几个小时的代码了。htmldata.replace()
的结果也需要重新分配吗?@pero啊,是的,需要。我应该把整行写下来——我只写了替换部分。现在编辑!谢谢!它现在工作得很好,我已经花了好几个小时在看这个代码了。