Как вывести строки содержащие слово?

Как вывести строки которые содержат слово "error"?

const fs = require('fs')
const file = fs.readFileSync('./history.txt', 'utf8')
var regexp = /error/g;
var match, matches = []

while ((match = regexp.exec(file)) != null) {
    matches.push(match.index)
}

console.log(matches)

Ответы (1 шт):

Автор решения: vsemozhebuty

Попробуйте так:

const fs = require('fs')
const file = fs.readFileSync('./history.txt', 'utf8')
const regexp = /^.*\berror\b.*$/mg;
const matches = file.match(regexp) || [];

console.log(matches);
→ Ссылка