admin管理员组

文章数量:1426021

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"

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"
Share Improve this question edited Apr 5, 2011 at 21:40 donald asked Apr 5, 2011 at 21:28 donalddonald 23.8k45 gold badges145 silver badges224 bronze badges 0
Add a ment  | 

2 Answers 2

Reset to default 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

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));

本文标签: javascriptHow to Parse an Array in NodejsStack Overflow