Javascript Node.js:获取“From:”和“To:”的正则表达式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5675315/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Node.js: Regular Expression To Get "From: " and "To: "
提问by donald
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: [email protected]
To: [email protected]
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).
我想获取每个字段(返回路径、日期、发件人、收件人等)以及正文(“testing123)”。
I've tried matching using:
我试过匹配使用:
var bodyRegex = /[\n]Subject: (.+)[\n](.+)/
but I get empty value.
但我得到空值。
回答by Dmitrij Golubev
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
回答by Simon H?nisch
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):
要获取每个标头及其值,有一个非常简单的正则表达式 ( http://regexr.com/3e60k) 和两个捕获组,它还允许在值内换行(如果缩进):
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:
获取正文(http://regexr.com/3e60h)更简单,因为它必须与带有两个换行符的标题分开:
var body = string.match(/\n\n([\s\S]+)/)[1];
That matches anything after the two \n
(whitespace and non-whitespace).
这匹配两者之后的任何内容\n
(空格和非空格)。
See this fiddle for a complete example: http://es6fiddle.net/issocwc9/
有关完整示例,请参阅此小提琴:http: //es6fiddle.net/issocwc9/