What I would like to do is read through the file like the one below and save all the function names into an array using regex. The function names I would like to save would be 'firstCall', 'SecondCall'. Ive tested the regex pattern and it seems to be working. But the issue is how do I use the regex pattern to search through the data from the text file I read before? If this is impossible would I be able to do the following task using maybe shell script?
function firstCall(){
some random logic
}
function SecondCall(data){
some other random logic
}
So far I have the following code where it would take the filename as an argument and then use readFile to search through that file. I wanted to be able to run this script on a terminal, which is why I have the file name passed in as an argument.
const fs = require("fs");
let fileName = process.argv[2];
const reg_pattern = /(?<=function )s*[a-zA-Z]*/g;
console.log(fileName);
fs.readFile(fileName, (err, data) => {
if (err) throw err;
let functionName = reg_pattern.exec(data);
//let functionName_2 = data.match(reg_pattern);
console.log(functionName);
});
This is what the output looks like as of now. The first position of the array is 'firstCall' but the rest doesn't seem to look right. Im not really sure whats going wrong.
$ node testing.js testing.txt
testing.txt
[
'firstCall',
index: 9,
input: 'function firstCall(){\r\n' +
' some random logic\r\n' +
'}\r\n' +
'\r\n' +
'\r\n' +
'function SecondCall(data){\r\n' +
' some other random logic\r\n' +
'}',
groups: undefined
]