Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Node.js: Regular Expression To Get "From: " and "To: "

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

I've tried matching using:

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

but I get empty value.

like image 218
donald Avatar asked Apr 15 '11 10:04

donald


People also ask

What is ?! In regex?

Definition and Usage. The ?! n quantifier matches any string that is not followed by a specific string n. Tip: Use the ?= n quantifier to match any string that IS followed by a specific string n.

What is \r and \n in regex?

Matches a form-feed character. \n. Matches a newline character. \r. Matches a carriage return character.

What does * do in regex?

The Match-zero-or-more Operator ( * ) This operator repeats the smallest possible preceding regular expression as many times as necessary (including zero) to match the pattern. `*' represents this operator. For example, `o*' matches any string made up of zero or more `o' s.


1 Answers

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
like image 188
Dmitrij Golubev Avatar answered Oct 29 '22 18:10

Dmitrij Golubev