파일을 읽고 해당 파일의 일부 텍스트를 동적 content.when로 바꾸어야합니다. 파일에서 읽은 데이터에 대해 string.replace가 작동하지 않았습니다. . 그러나 문자열에 대해서는 작동 중입니다. node.js를 사용하고 있습니다.node.js 익스프레스 서버에서 string.replace가 작동하지 않습니다.
fs.readFile('test.html', function read(err, data) {
if (err) {
console.log(err);
}
else {
var msg = data.toString();
msg.replace("%name%", "myname");
msg.replace(/%email%/gi, '[email protected]');
temp = "Hello %NAME%, would you like some %DRINK%?";
temp = temp.replace(/%NAME%/gi,"Myname");
temp = temp.replace("%DRINK%","tea");
console.log("temp: "+temp);
console.log("msg: "+msg);
}
});
출력 : 당신이 그것을 액세스하기 위해 변수에 그 할당해야하므로
temp: Hello Myname, would you like some tea?
msg: Hello %NAME%, would you like some %DRINK%?
감사합니다 .. : 귀하의 경우에, 당신은
msg = msg.replace("%name%", "myname");
코드처럼 할 필요가 – Damodaran