Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save prograhammer/4d1d642a58071599e5e8475fd65bc7e1 to your computer and use it in GitHub Desktop.
Save prograhammer/4d1d642a58071599e5e8475fd65bc7e1 to your computer and use it in GitHub Desktop.
Apply Regex to all files in directory, using node js
var fs = require('fs');
function readFiles(dirname, onFileContent, onError) {
fs.readdir(dirname, function(err, filenames) {
if (err) {
onError(err);
return;
}
filenames.forEach(function(filename) {
fs.readFile(dirname + filename, 'utf-8', function(err, content) {
if (err) {
onError(err);
return;
}
onFileContent(filename, content);
});
});
});
}
readFiles('./', function(filename, content) {
console.log(filename);
// var newcontent=content.replace(/\n"\nauthor/,"\nauthor");
var newcontent=content.replace(/title:\s*"([^"]*)\nauthor/,"title: \"$1\"\nauthor");
fs.writeFile(filename,newcontent,{encoding:'utf8'},
function(){
console.log('OK '+filename);
});
}, function(err) {
throw err;
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment