In nodejs is it possible to detect multiple occurrences of the same string in a txt file?
My current code is as below
const fs = require('fs');
var file_path = 'file.txt';
fs.readFile(file_path, "UTF-8", (error, data) => {
if (error) throw error;
else {
if (data.includes('Test Value')) {
console.log(data.indexOf('Test Value'))
}
fs.close(file, (err) => {
if (err)
console.error('Failed to close file', err);
else {
console.log("\n> File Closed successfully");
}
});
}
});
In file.txt, I have below contents
Value1
Value2
Test Value
Value3
Test Value
Value4
when I run the above code, I could only detect first occurrence of 'Test Value' whereas I need to detect all occurrences of 'Test Value' in file.txt, please help