how would i search a file for a string javascript in javascript
index.tsx
const fs = require('fs');
const searchFile = (filePath, searchString) => {
// Read content of the file fs.readFile(filePath, 'utf8', (err, data) => {
if (err) {
console.error(err);
return;
}
// Search for the string in the fileif (data.includes(searchString)) {
console.log(`String '${searchString}' found in the file.`);
} else {
console.log(`String '${searchString}' not found in the file.`);
}
});
};
// Usageconst filePath = 'example.txt'; // Path to the fileconst searchString = 'javascript'; // String to searchsearchFile(filePath, searchString);
668 chars
24 lines
This code uses the Node.js fs module to read the content of the specified file and then checks if the file contains the specified string.