Skip to content

Instantly share code, notes, and snippets.

@loujaybee
Created January 10, 2016 14:42
Show Gist options
  • Save loujaybee/fb0cb9825c0d81121bb9 to your computer and use it in GitHub Desktop.
Save loujaybee/fb0cb9825c0d81121bb9 to your computer and use it in GitHub Desktop.
Extracting html in node
var Promise = require('bluebird');
var fs = Promise.promisifyAll(require('fs'));
var runRegex = function(html) {
var extract_test_data = /(status_)(passed|failed)(.{16})([a-z]+)([0-9]+)(.{2})([0-9]{3})/gm;
var output = {};
while ((result = extract_test_data.exec(html)) != null) {
output[result[7]] = result[2];
}
return output
};
var logToConsole = function(result){
console.log(JSON.stringify(result));
};
var parseHtmlForStatusReport = (function() {
return fs.readFileAsync('./example.html', 'utf-8')
.then(runRegex)
.then(logToConsole)
.then(process.exit);
})();
/*
Result:
{
"155": "failed",
"003": "passed",
"019": "failed",
"022": "failed",
"025": "failed"
}
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment