Node.js:正则表达式获取“From:”和“To:”

时间:2011-04-15 10:22:06

标签: javascript regex node.js

鉴于此文本文件:

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

我想得到每一个字段(Return-path,Date,From,To等)以及body(“testing123”)。

我尝试使用匹配:

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

但我得到空值。

2 个答案:

答案 0 :(得分:20)

试试这个:

<强>代码:

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

var arrMatches = strText.match(rePattern);

<强>结果:

arrMatches[0] -> Subject: test
arrMatches[1] -> test

答案 1 :(得分:3)

这个问题刚刚向我建议(即使它已经很老了!?)我认为接受的答案并没有完全符合要求(得到每个字段+正文),所以我以为我会分享这个...

要获取每个标头及其值,有一个非常简单的正则表达式(http://regexr.com/3e60k),它有两个捕获组,也允许在一个值内换行(如果缩进):

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

可以像

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

获取正文(http://regexr.com/3e60h)更简单,因为必须使用两个换行符将其与标题分开:

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

匹配两个\n之后的任何内容(空白和非空白)。

请参阅此小提琴以获取完整示例:http://es6fiddle.net/issocwc9/