0

I'm trying to create a function that retrieves a specific URL from an email HTML body.

The URL that I'm trying to retrieve with a regular expression has this form

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

I've tried this regular expression but it always print me "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 ?

2 Answers 2

1

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 = new RegExp(regx)

But the way you’re doing it you’re passing a regular expression object and not a regular expression string.

try that way:

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

Sign up to request clarification or add additional context in comments.

Comments

1

This also appears to work...

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

or

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.