Looking to scrape the comments out of a JS file. Was thinking I can create a function to input a .js file, perform a RegExp match, and output an array of strings using fs.readFile() and string.match();
Here's an over-simplified example:
I have two files class.js (to read) and parse.js (to perform the text parsing)
class.js:
/*
by: Mike Freudiger
*/
/**
* one
* @returns 'Hello World'
*/
function one () {
return 'Hello World';
}
alert();
/* end of file */
parse.js:
var fs = require('fs');
var file = fs.readFile('C:\\Users\\mikef\\Desktop\\node_regex_test\\class.js', 'utf8', function(err, doc) {
var comments = doc.match(/(\/\*\*(.|\n)+?\*\/)/g);
console.log(comments);
});
when I run node parse.js the console output is null.
However when I run the regex match on a multiline string, I get the expected output:
var doc = `/*
by: Mike Freudiger
*/
/**
* one
* @returns 'Hello World'
*/
function one () {
return 'Hello World';
}
alert();
/* end of file */`
Any idea why the readFile() string would behave differently than a string literal?
...Also, I realize there may be a better way to get these comments out, with another npm package or something, but now I really just want to know why these two strings are different.