如何在字之间或行之间进行搜索?

如何找到文件中单词之间或“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);

    }

})


慕运维8079593
浏览 75回答 1
1回答

绝地无双

您可以对文件进行切片:const startIndex = file.indexOf(startString) + startString.length;const endIndex = file.indexOf(endString);const between = file.slice(startIndex, endIndex);console.log(between);
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript