Skip to content

Instantly share code, notes, and snippets.

@hdf
Created August 9, 2014 19:16
Show Gist options
  • Save hdf/d0de1c0df3fecd19cf50 to your computer and use it in GitHub Desktop.
Save hdf/d0de1c0df3fecd19cf50 to your computer and use it in GitHub Desktop.
incremental regex replace in nodejs
var fs = require('fs');
var file = 'EoCApp.CT';
if (process.argv.length > 2)
file = process.argv[2];
var f = fs.readFileSync(file, 'utf8');
var i = 0;
f = f.replace(/<ID>\d+<\/ID>/gm, function () {return '<ID>' + i++ + '</ID>'});
l = file.lastIndexOf('.');
var file2 = file.substring(0, l) + '2.' + file.substring(l + 1);
fs.writeFileSync(file2, f);
console.log(file2 + " written.");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment