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

ぃ、小莉子 提交于 2019-12-04 19:29:08

问题


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.


回答1:


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



回答2:


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/



来源:https://stackoverflow.com/questions/5675315/node-js-regular-expression-to-get-from-and-to

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!