1 回答
TA贡献1807条经验 获得超9个赞
我希望我理解正确
const file = fs.readFileSync('./history.txt', 'utf8');
const startString = '-----CompilerOutput:-stderr----------';
const endString = '-----EndCompilerOutput---------------';
var startIndex = file.indexOf(startString);
while(startIndex > -1) {
// get index after '-----CompilerOutput:-stderr----------'
let start = startIndex + startString.length;
// -----------------------------------
// EDIT: WRONG --> let end = file.indexOf(endString);
// Need to start searching for next occurence starting at index after the start
// -----------------------------------
// get index before '-----EndCompilerOutput---------------'
let end = file.indexOf(endString, start);
// get the text between
console.log(file.slice(start, end));
// set startIndex to next index of '-----CompilerOutput:-stderr----------'
// if not found the startIndex value will be -1 so end of the while loop
startIndex = file.indexOf(startString, end + endString.length);
}
编辑:
示例文件:
-----CompilerOutput:-stderr----------
test1
-----EndCompilerOutput---------------
-----CompilerOutput:-stderr----------
test2
-----EndCompilerOutput---------------
输出:
test1
test2
添加回答
举报