var http = require('http');
var urlOpts = {host: 'www.nodejs.org', path: '/', port: '80'};
http.get(urlOpts, function (response) {
response.on('data', function (chunk) {
var str=chunk.toString();
var re = new RegExp("(<\s*title[^>]*>(.+?)<\s*/\s*title)\>", "g")
console.log(str.match(re));
});
});
Output
user@dev ~ $ node app.js [ ‘node.js’ ] null null
I only need to get the title.
I would suggest using
RegEx.execinstead ofString.match. You can also define the regular expression using the literal syntax, and only once:The code also assumes that the
titlewill be completely in one chunk, and not split between two chunks. It would probably be best to keep an aggregation of chunks, in case thetitleis split between chunks. You may also want to stop looking for thetitleonce you’ve found it.