替换node.js中的文本文件中的字符串 [英] Replace string in a text file in node.js

查看:542
本文介绍了替换node.js中的文本文件中的字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用node.js.我想读取带有一些占位符字符串的文件,并在我提供文件之前动态替换它们。这不是HTML文件,因此模板引擎无效。

I am using node.js. I want to read a file with some placeholder strings and replace them dynamically before I serve the file. This is not an HTML file, so a templating engine will not work.

我该怎么做?

推荐答案

如果模板引擎过度使用,只需使用 string.replace()

If a template engine is overkill just use string.replace().

temp = "Hello %NAME%, would you like some %DRINK%?";

temp = temp.replace("%NAME%","Michael Dillon");
temp = temp.replace("%DRINK%","tea");
console.log(temp);

只需要做多一点工作就可以根据标准方法制作通用模板功能String对象。

With only a bit more work you could make a general purpose template function based on just the standard methods in the String object.

这篇关于替换node.js中的文本文件中的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆