简体   繁体   English

从 Google App Script 中的 HTML 中提取 URL

[英]Extract URL from HTML in Google App Script

I'm trying to create a function that retrieves a specific URL from an email HTML body.我正在尝试创建一个 function 从 email Z4C4ZDBAD5FCA2E78A1A3DZ 中检索特定的 URL。

The URL that I'm trying to retrieve with a regular expression has this form我试图用正则表达式检索的 URL 具有这种形式

https://dashboard.stripe.com/emails/receipts/invrc_1GYYpBFlgHQ8OfGyspnJivUe/pdf https://dashboard.stripe.com/emails/receipts/invrc_1GYYpBFlgHQ8OfGyspnJivUe/pdf

So it has this pattern fixed https://dashboard.stripe.com/emails/receipts/invrc_ + alphanumeric string of 24 characters + fixed /pdf所以它有这个模式固定https://dashboard.stripe.com/emails/receipts/invrc_ + 24 个字符的字母数字字符串 + 固定 /pdf

I've tried this regular expression but it always print me "null"我试过这个正则表达式,但它总是打印我“null”


var threads = GmailApp.search('subject:"Your receipt from ‪Weglot‬"',0,1)[0];
var messages = threads.getMessages();
var body = messages[0].getBody();

      var url = new RegExp(/https:\/\/dashboard.stripe.com\/emails\/receipts\/invrc_/+/[a-zA-Z0-9]+/+/\/pdf/)

      var data = body.match(url)
      Logger.log(data)
  }

Has someone got an idea to fix this regular expression?有人有解决这个正则表达式的想法吗?

You don't need a constructor to create this regular expression, the constructor's idea is when you want to convert a string into a regular expression, for example:您不需要构造函数来创建此正则表达式,构造函数的想法是当您要将字符串转换为正则表达式时,例如:

let regx = "\d{4}"让 regx = "\d{4}"

regx = new RegExp(regx) regx = 新的正则表达式(regx)

But the way you're doing it you're passing a regular expression object and not a regular expression string.但是你这样做的方式是传递正则表达式 object 而不是正则表达式字符串。

try that way:试试这样:

let url = /https://dashboard.stripe.com/emails/receipts/invrc.+?/pdf/让 url = /https://dashboard.stripe.com/emails/receipts/invrc.+?/pdf/

This also appears to work...这似乎也有效......

var url = new RegExp(/https://dashboard.stripe.com/emails/receipts/invrc_[az,AZ,0-9]*/pdf/); var url = new RegExp(/https://dashboard.stripe.com/emails/receipts/invrc_[az,AZ,0-9]*/pdf/);

or或者

var url = new RegExp(/https://dashboard.stripe.com/emails/receipts/invrc_[az,AZ,0-9]{24}/pdf/); var url = new RegExp(/https://dashboard.stripe.com/emails/receipts/invrc_[az,AZ,0-9]{24}/pdf/);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM