How to extract URL from email body using JavaScript and Regex?

King Java 500 Reputation points
2024-10-17T01:38:43.31+00:00

I am posting this for the third time because for the last two times, I had an issue with getting the right Regex (ECMAScript) for the URL that I was trying to convert (from email body), but it appears that this Regex should be good one as I verified with Regex101.

User's image

I have Logic App that looks like bottom:

User's image

I am trying to get part of email text like below:

originalsrc="https://portal.carefeed.com/xxxxx/reports/report/emailReport?details=60a286e8-d616-452c-87a5-65ccff6f01cb"

Why do I get "InvalidCodeScriptRunTimeFailure"?

Bottom is JavaScript that I was using on the third process (Execute JavaScript Code).

const jsonString  = workflowContext.trigger.outputs.body.bodyPreview;
const jsonObject = JSON.parse(JSON.stringify(jsonString))
const regex = /originalsrc=\\"https:\/\/(www\.)?\w+(\.\w+).*$"/;
   
const match = jsonObject.match(regex);
   
   if (match) {
       return "Matched"
   } else {
       return "No Match"
   }

Bottom are two posts that I asked:

https://learn.microsoft.com/en-us/answers/questions/1696123/how-to-extract-url-link-from-email-body-using-azur

https://learn.microsoft.com/en-us/answers/questions/1684590/extract-url-link-behind-email-body-using-azure-log

And this is article that I got some clue to start with:

https://learn.microsoft.com/en-us/answers/questions/1350491/extract-url-link-behind-email-body-using-logicapps

Thank you.

Azure Logic Apps
Azure Logic Apps
An Azure service that automates the access and use of data across clouds without writing code.
3,204 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Khadeer Ali 660 Reputation points Microsoft Vendor
    2024-10-23T01:50:12.5833333+00:00

    Hi King Java,

    Welcome to the Microsoft Q&A Platform! Thank you for asking your question here.

    The error "InvalidCodeScriptRuntimeFailure" you're encountering likely stems from how you're trying to access the email body within the JavaScript code. The provided code snippet assumes there's a bodyPreview property within the body object of the workflowContext.trigger.outputs.

    The workflowContext.trigger.outputs.body.bodyPreview property might not be the correct way to access the email body text depending on the specific environment you're running the code in.

    Even if the bodyPreview property contains the email body, the code snippet attempts to parse it as JSON twice using JSON.parse(JSON.stringify(jsonString)). This is unnecessary and might be causing errors if the email body isn't valid JSON.

    Here's an improved version of the JavaScript code assuming you have a body property containing the email text directly:

    const emailBody = workflowContext.trigger.outputs.body; // Assuming this holds the email body text
    const regex = /originalsrc="https:\/\/(www\.)?\w+(\.\w+).*$"/;
    const match = emailBody.match(regex);
    
    if (match) {
      return match[1]; // Assuming you only want the URL itself (without the originalsrc=")
    } else {
      return "No Match";
    }
    
    
    
    

    If you find this answer helpful, please click "Accept Answer" and kindly upvote it.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.