search文件的关键字,然后logging行的其余部分

我在这里浏览了与我的问题有关的其他问题,但他们不适合我的情况,或者我错误地使用它,需要更好的解释。

我正在阅读一个大文件,看第一个单词,看它是否匹配来自用户的input,如果是这样,我想console.log的其余部分。

FILE.TXT

 #one: This is the first line #two: This is the second line #three: This is the third line etc 

节点Js

 // Take in user input var msgSplit = userInput.split(" "); if (msgSplit[0].startsWith("#") { var lineReader = require('readline').createInterface({ input: require('fs').createReadStream('Custom_Phrases') }); lineReader.on('line', function(line) { if(line.indexOf(msgSplit[0]) < 0) { console.log(line); } }); } 

这工作,种,但它返回后,我想要的行。 如果可能,我想例如,如果用户input#one,这是第一行logging到控制台。

line.indexOf(msgSplit[0]) < 0表示没有find您的用户input。 因此只显示了与用户input不匹配的行。

尝试这个:

 // Take in user input var msgSplit = userInput.split(" "); if (msgSplit[0].startsWith("#") { var lineReader = require('readline').createInterface({ input: require('fs').createReadStream('Custom_Phrases') }); lineReader.on('line', function(line) { if(line.indexOf(msgSplit[0]) >= 0) { console.log(line); } }); }