如何找到文件中单词之间或“startString”和“endString”行之间的所有行?const fs = require('fs')const startString = '-----CompilerOutput:-stderr----------'const endString = '-----EndCompilerOutput---------------'const file = fs.readFileSync('./history.log', 'utf8')let arr = file.split(/\r?\n/);arr.forEach((line, idx)=> { if(line.includes(startString, endString)){ console.log((idx+1)+':'+ line); }})
1 回答
绝地无双
TA贡献1946条经验 获得超4个赞
您可以对文件进行切片:
const startIndex = file.indexOf(startString) + startString.length;
const endIndex = file.indexOf(endString);
const between = file.slice(startIndex, endIndex);
console.log(between);
添加回答
举报
0/150
提交
取消