Javascript 使用node.js将文本文件中的正则表达式替换为文件内容
这个问题将适用于任何文本文件,但由于我想用它替换HTML,我将使用HTML文件作为示例。我看过npm上的“吞咽注入”和“替换”之类的东西,但这两种方法都没有达到我所需要的效果 我想要一些引用其他文件的占位符文本。运行此替换函数时,placeholdler文本将替换为文件内容 main.htmlJavascript 使用node.js将文本文件中的正则表达式替换为文件内容,javascript,regex,node.js,build,Javascript,Regex,Node.js,Build,这个问题将适用于任何文本文件,但由于我想用它替换HTML,我将使用HTML文件作为示例。我看过npm上的“吞咽注入”和“替换”之类的东西,但这两种方法都没有达到我所需要的效果 我想要一些引用其他文件的占位符文本。运行此替换函数时,placeholdler文本将替换为文件内容 main.html <script><replace src="./other.js" /></script> 转换后,输出文件应为 <script>console.log(
<script><replace src="./other.js" /></script>
转换后,输出文件应为
<script>console.log("Hello, world!")</script>
console.log(“你好,世界!”)
我已经了解了以下内容,但不知道如何使其与节点中的文件流一起工作
var REGEX = /<replace src="(.+)" \/>/;
function replace(file){
match = file.match(REGEX);
var placeholder = match[0];
if (placeholder) {
return file.replace(placeholder, match[1].toUpperCase());
// toUpperCase is just an example and instead should lookup a file for contents
}
}
var REGEX=/;
函数替换(文件){
match=file.match(REGEX);
变量占位符=匹配[0];
如果(占位符){
返回文件.replace(占位符,匹配[1].toUpperCase());
//toUpperCase只是一个示例,它应该查找文件中的内容
}
}
如果您的文件大小合理,您可以避免使用流,并为string.replace()使用自定义替换程序。
:
var fs=require('fs');
函数替换(路径){
var REGEX=//g;
//加载html文件
var fileContent=fs.readFileSync(路径“utf8”);
//replacePath是您的匹配项[1]
fileContent=fileContent.replace(REGEX,函数replacer(match,replacePath){
//加载并返回替换文件
返回fs.readFileSync(replacePath,'utf8');
});
//这将覆盖原始html文件,更改测试路径
fs.writeFileSync(路径,文件内容);
}
替换('./main.html');
使用流
var es = require('event-stream');
function replaceWithStreams(path) {
var REGEX = /<replace src="(.+)" \/>/g;
fs.createReadStream(path, 'utf8')
.pipe(es.split()) // split the input file into lines
.pipe(es.map(function (line, next) {
line = line.replace(REGEX, function replacer(match, replacePath) {
// better to keep a readFileSync here for clarity
return fs.readFileSync(replacePath, 'utf8');
});
next(null, line);
})).pipe(fs.createWriteStream(path)); // change path if needed
}
replaceWithStreams('./main.html');
var es=require('event-stream');
函数替换为流(路径){
var REGEX=//g;
fs.createReadStream(路径“utf8”)
.pipe(es.split())//将输入文件拆分为行
.pipe(es.map)(函数(行,下一个){
line=line.replace(REGEX,函数replacer(match,replacePath){
//为了清晰起见,最好在此处保持readFileSync
返回fs.readFileSync(replacePath,'utf8');
});
下一步(空,行);
})).pipe(fs.createWriteStream(路径));//根据需要更改路径
}
replaceWithStreams('./main.html');
您可以尝试以下方法:
基本上,您将读取index.html,找到replace标记,读取文件中的内容,并替换为其他文件中的内容
这个片段是为了帮助您处理异常=D如果我包含的文件太大怎么办。。。如何更改
返回fs.readFileSync(replacePath,'utf8')代码>取而代之的是流吗?这可能吗?
var fs = require('fs');
function replace(path) {
var REGEX = /<replace src="(.+)" \/>/g;
// load the html file
var fileContent = fs.readFileSync(path, 'utf8');
// replacePath is your match[1]
fileContent = fileContent.replace(REGEX, function replacer(match, replacePath) {
// load and return the replacement file
return fs.readFileSync(replacePath, 'utf8');
});
// this will overwrite the original html file, change the path for test
fs.writeFileSync(path, fileContent);
}
replace('./main.html');
var es = require('event-stream');
function replaceWithStreams(path) {
var REGEX = /<replace src="(.+)" \/>/g;
fs.createReadStream(path, 'utf8')
.pipe(es.split()) // split the input file into lines
.pipe(es.map(function (line, next) {
line = line.replace(REGEX, function replacer(match, replacePath) {
// better to keep a readFileSync here for clarity
return fs.readFileSync(replacePath, 'utf8');
});
next(null, line);
})).pipe(fs.createWriteStream(path)); // change path if needed
}
replaceWithStreams('./main.html');