Mornor Mornor - 1 year ago 94
Javascript Question

Parse specific String Javascript (Node.js)

I would like to parse the following String:

date: [ 'Thu, 28 Apr 2016 10:56:13 +0200' ],
subject: [ 'Subject' ],
from: [ 'Blob <>' ],
to: [ '<>' ]

In order to access the variable
etc ...

But I am not sure how to do it since it

  1. Is not a valid JSON

  2. It is not a structure I know

And I don't want to re-invent the wheel if a solution exist which I am not (yet) aware of.

Any ideas?


Data are getting using a node-imap module (only relevant part)

f.on('message', function(msg, seqno) {
console.log('Message #%d', seqno);
var prefix = '(#' + seqno + ') ';
msg.on('body', function(stream, info) {
var buffer = '';
stream.on('data', function(chunk) {
buffer += chunk.toString('utf8');
stream.once('end', function() {
var parsedHeader = inspect(Imap.parseHeader(buffer));
console.log('Author: '+parsedHeader);


See the comment of @stdob--. Imap.parseHeader() return an object.

Answer Source

Looks like that Imap.parseHeader already returns an object with keys

Try console.log( Object.keys(parsedHeader) to see all the keys.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download