Google apps script, getBody() from Gmail, regex \\

2019-08-01 23:36发布

问题:

Good evening.
Have such problem with regex new-line character ("\n") in Google app script. I'm using .getbody() method for mail parse and get some of this

<td valign="middle" width="43" align="left">
<img src="http://img/mail-new-oct/icon2.png" style="display: block; vertical-align: middle;" alt="Logo">
</td>
<td align="center" valign="top" style="font-family: 'Open Sans',  Helvetica, 'Helvetica Neue', Arial, sans-serif; font-size: 13px; line-height: 30px; color: #000000; font-weight: 400; text-align: left;">0972398182
</td>

For this part, i need phone number 0972398182. I try get it with this expression:

icon2.*\n.*\n.*>\s*([\s\S]+?)\s*(?=<\/td>)

icon2 - is one unique identification for this html body, so i need to use them.
Go through without ("\n") impossible in this way, and finally it doesnt work. I read, that GAS doesnt work with ("\n") , but maybe have other way how to solve this?
Will be grateful for any help, thanks!

回答1:

You seem to have a combination of newlines and carriage returns in your input string. That means you may use [\r\n]+ character class with + quantifier:

icon2.*[\r\n]+.*[\r\n]+.*left.*>\s*(.+?)\s*(?=<\/td>)


回答2:

Please try the given workaround in Issue #13 logged in google-apps-script-issues tracker. Use:

\\n.

In addition to that, you may want to also check solutions given in the following posts:

  • \n not working in google apps script Browser.msgBox
  • How to add line breaks in a cell before specific keywords in Google Sheets