I want to get all the content in a text file before the first empty line.
I've found a working regex, but when I try to accomplish the same in Javascript it doesn't work.
(loading the file's contents is working)
async function readDir() {
return new Promise((resolve,reject) => {
fs.readdir('./content', (err, files) => {
if(err) { reject(err) }
resolve(files)
});
});
}
readDir().then((files) => {
files.forEach(file => {
var filepath = path.resolve('./content/'+file)
if(filepath.endsWith('.txt')) {
if(fs.statSync(filepath)["size"] > 0) {
let data = fs.readFileSync(filepath).toString();
let reg = /^[\s\S]*?(?=\n{2,})/;
console.log(data.match(reg)) //returns null
}
}
});
})
EDIT:
As O. Jones pointed out, the problem lies with the line endings. My regex was not picking up on \r\n
line endings present in my file.
For now, this one seems to do the job: /^[\s\S]*?(?=(\r\n\r\n?|\n\n))/m