Having problem with regex match capturing on Google Scripts

Viewed 43
<html>
<head>
<style>
* {
font-family:Roboto, "Helvetica Neue", Helvetica, Arial, sans-serif;
}
</style>
</head>
<body>
<table cellpadding="0" cellspacing="0" class="email-wrapper" style="padding-top:32px;background-color:#ffffff;"><tbody>
<tr><td>
<table cellpadding=0 cellspacing=0><tbody>
<tr><td style="max-width:560px;padding:24px 24px 32px;background-color:#fafafa;border:1px solid #e0e0e0;border-radius:2px">
<img style="padding:0 24px 16px 0;float:left" width=72 height=72 alt="Error Icon" src="cid:icon.png">
<table style="min-width:272px;padding-top:8px"><tbody>
<tr><td><h2 style="font-size:20px;color:#212121;font-weight:bold;margin:0">
Message blocked
</h2></td></tr>
<tr><td style="padding-top:20px;color:#757575;font-size:16px;font-weight:normal;text-align:left">
Your message to <a style='color:#212121;text-decoration:none'><b>email@email.com</b></a> has been blocked. See technical details below for more information.
</td></tr>
<tr><td style="padding-top:24px;color:#4285F4;font-size:14px;font-weight:bold;text-align:left">
<a style="text-decoration:none" href="https://support.google.com/mail/answer/69585">LEARN MORE</a>
</td></tr>
</tbody></table>
</td></tr>
</tbody></table>
</td></tr>
<tr style="border:none;background-color:#fff;font-size:12.8px;width:90%">
<td align="left" style="padding:48px 10px">
The response was:<br/>
<p style="font-family:monospace">
Message rejected. See https://support.google.com/mail/answer/69585 for more information.
</p>
</td>
</tr>
</tbody></table>
</body>
</html>

i need to parsing status text which always changed from html example above , im trying using regex capturing group. but i cannot make it work. the regex pattern seem work on regex101

function myFunction() {
// Get the first message in the first thread of your inbox
var message = GmailApp.getInboxThreads(0, 1)[0].getMessages()[0];
var subject = message.getSubject();

Logger.log(body)
Logger.log(/\<p\sstyle\=\"font\-family\:monospace\"\>\r(.*)/.exec(body))
Logger.log(body.match(/\<p\sstyle\=\"font\-family\:monospace\"\>\r(.*)/))

im also try 2 different regex as i found on internet solution the output is


    [<p style="font-family:monospace">, ]

need to capture constantly changed text which currently 'Message blocked'. any help will be very much appreciated .

edited : i found the problem. which is google script regex cannot read carriage return that's why it cannot capture my regex code. so I need to replace all /n and /r on my output and do regex. the code is

    var body = message.getBody(); // capture output
    var bodyText = body.replace(/[\r\n]/g, ""); // replace all carriage return to make regex work. 

    var regExp = new RegExp('weight:bold;margin:0\">(.*)<\/h2>', "gm");
    var statusInfo = regExp.exec(bodyText)[1]; //capture the output group

1 Answers

Why not simply:

body.match(/Message.+/g)

7:03:32 AM  Notice  Execution started
7:03:34 AM  Info    [ 'Message blocked',
  'Message rejected. See https://support.google.com/mail/answer/69585 for more information.' ]
7:03:32 AM  Notice  Execution completed
Related