how would search a file for a string in javascript and save to array in javascript

index.tsx
const fs = require('fs');

// Read file content
const fileContent = fs.readFileSync('filename.txt', 'utf8');

// Search for a string in the file
const searchString = 'example';
const matches = fileContent.match(new RegExp(searchString, 'g'));

// Save matches to an array
const matchesArray = (matches !== null) ? matches : [];

console.log(matchesArray);
356 chars
14 lines

related categories

gistlibby LogSnag