How to grab second URL from email (how to modify this Java Script Code)?

King Java 665 Reputation points
2025-02-06T21:54:52.6666667+00:00

Hi, It took at least 6 months to figure this out, and now, due to the change of content from the source (email), I have to revise Java Script Code in Azure Logic App.

FYI, [this is the last post](https://learn.microsoft.com/en-us/answers/questions/2116991/how-to-extract-url-output-into-a-csv-file-(on-next?source=docs) that is related to this question.

Previously, I had only one URL in the email content as shown below:

User's image

Now, new email content has two URLs that I need to grab the second URL:

User's image

I am getting a wrong output:

User's image

This is Java Script that worked before:

var text = /https?:\/\/\S+/g;
var links = workflowContext.trigger.outputs.body.body.replace(/https:\/\/nam11.safelinks.protection.outlook.com[^"]+/g, '');
var result = links.match(text);

// Remove trailing double quote if it exists
if (result && result[0]) {
    result[0] = result[0].replace(/"$/, '');
}

return result[0];

User's image

The First URL (that has the text "Learn why this is important") starts like this in HTML:

<a href="https://aka.ms/LearnAboutSenderIdentification"

The Second URL (that has the text "View Admissions/Readmissions Report result") starts like this in HTML:

<a href="https://nam11.safelinks.protection.outlook.com/?

I guess I might have to modify the part, but I am not sure how.

I would appreciate for help!

var text = /https?:\/\/\S+/g;\
Azure Logic Apps
Azure Logic Apps
An Azure service that automates the access and use of data across clouds without writing code.
3,342 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Ryan Hill 29,651 Reputation points Microsoft Employee
    2025-02-11T20:51:55.22+00:00

    Hey @King Java

    I used an AI assistant to generate some code that will return the href of the anchor tag where the link text matches your last URL. I think that's probably the best way; but this code will fail should that inner text ever change.

    function getHrefFromAnchorTag(htmlString) {
        const regex = /<a\s+[^>]*href="([^"]*)"[^>]*>View Admissions\/Readmissions Report Results<\/a>/i;
        const match = htmlString.match(regex);
        let result = null;
        if (match && match[1]) {
            result = match[1];
        }
        return result;
    }
    
    // Example usage:
    const htmlString = '<a href="someurl">View Admissions/Readmissions Report Results</a>';
    const result = getHrefFromAnchorTag(htmlString);
    console.log(result); // Output: "someurl"
    

    This code is untested but hopefully will get you pointed in the right direction. In my opinion, you don't need to strip out the safelinks.outlook.com part of the url.

    If you know for certain that the link you need will always be the last one, then you can update the above snippet to match any anchor tag and take the last match.


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.