如何匹配从MTA开始的整行:?
如果我使用:
console.log(result[2].split("Reporting-MTA:"))
我仍然会得到剩余的一行,我想匹配从MTA开始的整行:
由于
编辑:
我希望匹配该行,以便我可以读取整行之前和之后的行。现在,我只能读取字符串“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]);
答案 0 :(得分:4)
答案 1 :(得分:4)
<强>代码:强>
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:
答案 2 :(得分:1)
您可以使用match来进行正则表达式匹配,并将匹配的结果数组返回。此正则表达式匹配以^
开头的模式(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
答案 3 :(得分:0)
更换您不想要的行可能更容易
var replaced = result[2].replace(/.*Reporting-MTA:.*/gm,'')
console.log(replaced)