0

What's the best way to parse:

[ 'Tue, 5 Apr 2011 15:15:59 +0100' ]
[ '[email protected]' ]
[ 'User Name <[email protected]>' ]
[ 'oi' ]

And take the [' '] out ?

Thanks

More details:

It's the heads of an IMAP e-mail.

msg.headers.date

returns the data, etc.

What I want is to have:

"Tue, 5 Apr 2011 15:15:59 +0100"
"[email protected]"
"User Name"
"[email protected]"
"oi"
0

2 Answers 2

3

So you're saying that console.log(msg.headers.date) gives you [ 'Tue, 5 Apr 2011 15:15:59 +0100' ]??

In that case, console.log(msg.headers.date[0]) == Tue, 5 Apr 2011 15:15:59 +0100

Is that what you're trying to get?


What is this? A file? Straight text? Part of a larger JSON structure?

Basically, convert it into an actual structure and load it, one way or another:

module.exports = [
   [ 'Tue, 5 Apr 2011 15:15:59 +0100' ],
   [ '[email protected]' ],
   [ 'User Name <[email protected]>' ],
   [ 'oi' ]
];

----

var info = require('./file');
// info[0][0] == Tue, 5 Apr 2011 15:15:59 +0100

or if you want to parse it:

var lines = [
   "[ 'Tue, 5 Apr 2011 15:15:59 +0100' ]",
   "[ '[email protected]' ]",
   "[ 'User Name <[email protected]>' ]",
   "[ 'oi' ]"
];

var info = JSON.parse('[' + lines.join(',') + ']');
// info[0][0] == Tue, 5 Apr 2011 15:15:59 +0100

Sign up to request clarification or add additional context in comments.

4 Comments

[ ... ] means it's an array, so adding [0] gives you the first element.
thanks. Can you please also say how to parse [ 'User Name <[email protected]>' ] to get "User name" and "Email" or do I need to open a new question?
it's a new question, but: var from = /(.*)?<(.*?)>/.exec(msg.headers.from[0]);. Now, from[1] == 'User Name' and from[2] == '[email protected]'
Thank you. I've voted up so you don't lose everything by not answering a new question.
1

Assuming each line is an element in the array lines:

var lines = [
    "[ 'Tue, 5 Apr 2011 15:15:59 +0100' ]",
    "[ '[email protected]' ]",
    "[ 'User Name <[email protected]>' ]",
    "[ 'oi' ]"
];

for(var i=0;i<lines.length;i++){
    lines[i]=lines[i].replace(/^\[ *'|' *\]$/g,'');
}

console.log(JSON.stringify(lines));

3 Comments

FYI, no alert method exists in node. console.log does
Sorry about my bad question. In fact, it's not a array of strings so that doesn't work. Thanks. Edit: It works if I convert toString().
@cwolves whoops, tested in jsfiddle and forgot to convert, thanks for the correction.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.