Node.js:正则expression式删除整条线

我如何匹配从MTA开始的整个行:?

如果我使用:

console.log(result[2].split("Reporting-MTA:")) 

我仍然会得到其余的部分,我想匹配从MTA开始的整个行:

谢谢

编辑:

我想匹配这条线,所以我可以阅读整条线之前和之后的行。 现在,我只能读取string“Reporting-MTA:”前后的行

EDIT2:

 2u+in.4eBZWuGLW.HROP9.bB7WHG content-type: message/delivery-status Reporting-MTA: dns; na-mm-outgoing-6102-bacon.iad6.com Final-Recipient: rfc822;g Action: failed Status: 5.0.0 (permanent failure) Diagnostic-Code: smtp; 5.1.2 - Bad destination host 'DNS Hard Error looking up tiagop.fdforg (MX): NXDomain' (delivery attempts: 0) /node/server.js:92 console.log(r[1]); ^ TypeError: Cannot read property '1' of null 

有了这个代码:

 console.log(result[2]); var r = result[2].match(/(.*)\r\nReporting-MTA\r\n(.*)/) console.log(r[1]); 

如果你的input是从一个文本文件,那么你需要寻找换行符,所以这样的事情…

它不漂亮,但..

testing:

http://jsfiddle.net/wBR73/2/

码:

 var re = new RegExp(/((?:[\n\r]|.)*)(Reporting-MTA.*)((?:[\n\r]|.)*)/); var arrMatches = strText.match(re); 

结果:

 arrMatches [1] // Lines before a line Reporting-MTA:..$ arrMatches [2] // Line started from Reporting-MTA: arrMatches [3] // Other lines after a line started from Reporting-MTA: 

您可以使用匹配来执行正则expression式匹配,并返回匹配结果数组。 这个正则expression式匹配一个以MTA:开始( ^ )的模式MTA:后面跟着任何( . )零次或多次( * )…

 var matches = result[2].match(/^MTA:.*/) console.log(matches[0]) 

捕捉匹配内的组可以使用括号…

 var matches = result[2].match(/^(MTA:)(.*)/) console.log(matches[0]) // whole matched string console.log(matches[1]) // first bracketed group - 'MTA:' in this case console.log(matches[2]) // second bracketed group - the rest of the string in this case 

也许更换你不想要的线更容易

 var replaced = result[2].replace(/.*Reporting-MTA:.*/gm,'') console.log(replaced)