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 file
        if (data.includes(searchString)) {
            console.log(`String '${searchString}' found in the file.`);
        } else {
            console.log(`String '${searchString}' not found in the file.`);
        }
    });
};

// Usage
const filePath = 'example.txt'; // Path to the file
const searchString = 'javascript'; // String to search
searchFile(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.

related categories

gistlibby LogSnag