【问题标题】:create files with node and replace variables with prompt input使用节点创建文件并使用提示输入替换变量
【发布时间】:2019-02-27 11:14:54
【问题描述】:

我用javascript写了一个小脚本,它会创建一些“.php”文件。

此文件的内容以 text.txt 格式存储,这是我生成文件的蓝图。

当我的脚本运行时,我想复制这些文件的内容并生成 .php 文件。这可行,但我想更改此文件中的一些变量。

例如文档中的名称。

“你好,变量名”

如何创建一个带有变量的 text.txt 文件,它将用我的 prompt() 输入替换变量?

$ 提示:姓名:Helena

所以在提示之后我希望我的文件看起来像:

“你好,海伦娜”

【问题讨论】:

  • 您可以使用一些预定义的字符串,如#something# 并将其替换为您想要的值,并创建您想要的文件
  • 显示文件有特殊的文档类型?我使用 .txt .. 我使用 #variable.name# 但我只得到我输入的字符串,而不是存储在我的数据对象中的值。我通过 fs.writeFileSync() 加载文件
  • 文件类型无所谓,将内容读取为字符串,然后进行操作
  • 好的,但你能给点建议吗?还是例子?

标签: javascript node.js prompt


【解决方案1】:

我为你搜索了一些链接

读取this以读取节点中文件的内容

var fs = require('fs');
var contents = fs.readFileSync('DATA', 'utf8');
console.log(contents);

阅读this替换文字

var p = 'The quick brown fox jumps over the lazy dog. If the dog reacted, was it really lazy?';

var regex = /dog/gi;

console.log(p.replace(regex, 'ferret'));
// expected output: "The quick brown fox jumps over the lazy ferret. If the ferret reacted, was it really lazy?"

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-08-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多