Node.js: Regular Expression To Get “From: ” and “T

2020-06-12 03:59发布

Given this text file:

Received: from unknown (HELO aws-bacon-delivery-svc-iad-1007.vdc.g.com) ([10.146.157.151])
  by na-mm-outgoing-6102-bacon.iad6.g.com with ESMTP; 12 Apr 2011 14:30:47 +0000
Return-Path: 0000012f4a2a0037-528dbafb-e773-44be-bef5-07d8f63e6aee-000000@email-bounces.g.com
Date: Tue, 12 Apr 2011 14:42:37 +0000
From: xxx@xxx.example.com
To: yyy@yyy.example.com
Message-ID: <0000012f4a2a0037-528dbafb-e773-44be-bef5-07d8f63e6aee-000000@email.g.com>
Subject: test
Mime-Version: 1.0
Content-Type: text/plain;
 charset=UTF-8
Content-Transfer-Encoding: 7bit
X-AWS-Outgoing: 199.255.192.79

testing123

I want to get every field (Return-path, Date, From, To, etc.) as well as the body ("testing123).

I've tried matching using:

    var bodyRegex = /[\n]Subject: (.+)[\n](.+)/

but I get empty value.

2条回答
▲ chillily
2楼-- · 2020-06-12 04:12

Try this:

Code:

//var rePattern = new RegExp(/^Received:(.*)$/);
var rePattern = new RegExp(/^Subject:(.*)$/);

var arrMatches = strText.match(rePattern);

Result:

arrMatches[0] -> Subject: test
arrMatches[1] -> test
查看更多
你好瞎i
3楼-- · 2020-06-12 04:27

This question was just suggested to me (even though it's quite old!?) and I think the accepted answer doesn't exactly do what was asked for (get every field + body), so I thought I'll share this...

To get each header and its value there is a quite simple regex (http://regexr.com/3e60k) with two capture groups, that also allows line breaks within a value (if indented):

var pattern = /(.+):\s(.+(?:\n +)?.+)?/g;

The pairs can be retrieved like

var match;
while (match = pattern.exec(string)) {
    console.log(match[1] + ": " match[2]);
}

It's even simpler to get the body (http://regexr.com/3e60h), because it has to be separated from the headers with two new-line characters:

var body = string.match(/\n\n([\s\S]+)/)[1];

That matches anything after the two \n (whitespace and non-whitespace).

See this fiddle for a complete example: http://es6fiddle.net/issocwc9/

查看更多
登录 后发表回答