How can I extract a particular link from a string of HTML in JavaScript?

Luke Wheeler

I'm using the Flickr public API to build a small Express and React app. It returns a 'description' which is a string of HTML:

<p>
    <a href="URL">martin_king.photo</a> posted a photo:
</p> 
<p>
    <a href="URL_IMAGE" title="Potato Planting 2018 | FENDT // REEKIE">
        <img src="URL_IMAGE" width="240" height="160" alt="DESC_PHOTO" />
    </a>
</p>
<p> 
    FENDT 412 Vario TMS with the REEKIE ScanStone Separator <br/><br/> Follow Us: <br/> • 
    <a href="URL" rel="nofollow">Facebook - DYNASTYphotography</a> <br/> • 
    <a href="URL" rel="nofollow">Lukaskralphoto Instagram</a><br/> • 
    <a href="URL" rel="nofollow">my personal Instagram</a><br/> • 
    <a href="URL" rel="nofollow">YouTube Channel</a>
</p>

The first a tag there contains the link I need, I just need some help writing a function/RegExp that can help me extract it. This is my intended result:

getFirstLinkFromDescription = string => ... // 1st 'URL'

Thanks for any help!

Titus

When it comes to parsing HTML, regex is not that precise. What you could do is to create Nodes from that HTML which will make it easier to access the different attributes of those elements, something like:

var html = '<p><a href="https://www.flickr.com/people/129661619@N08/">martin_king.photo</a> posted a photo:</p> <p><a href="https://www.flickr.com/photos/129661619@N08/41552236041/" title="Potato Planting 2018 | FENDT // REEKIE"><img src="https://farm1.staticflickr.com/899/41552236041_7183e7a533_m.jpg" width="240" height="160" alt="Potato Planting 2018 | FENDT // REEKIE" /></a></p> <p>FENDT 412 Vario TMS with the REEKIE ScanStone Separator<br /> <br /> You can follow us: <br /> • <a href="https://www.facebook.com/DYNASTYphotography-204551272942391/timeline/" rel="nofollow">Facebook - DYNASTYphotography</a> <br /> • <a href="https://www.instagram.com/lukaskralphoto/" rel="nofollow">Lukaskralphoto Instagram</a><br /> • <a href="https://www.instagram.com/martin939king/" rel="nofollow">my personal Instagram</a><br /> • <a href="https://www.youtube.com/user/HighlandBunnyLuv" rel="nofollow">YouTube Channel</a></p>';
var div = document.createElement("div");
div.innerHTML = html;
var anchor = div.querySelector("a");
console.log(anchor.href);

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

how to Extract Link part from a string in javascript

Java: how can I use Jsoup to extract a particular data from html?

How can I extract text from the middle of a string with Javascript?

How can I extract HTML from a URL that requires JavaScript?

How can I extract the nested value from this Javascript/HTML code?

How can I search for and extract a particular value from a dataframe in python?

How can I extract URL tags and link text from HTML in Perl?

How can I extract hashtags from string?

How can I extract tuples from a string?

How can i extract data from String?

How can I extract links from HTML?

How to extract a particular part from a string in bash

How to extract a particular pattern from a String

How to extract particular string from a file (linux)

how to extract a particular substring from a string in python

How can I extract the estimated home value from a Zillow link?

How Can i extract file name from a php download link

How do I Remove particular dublicate characters from string in javascript?

Javascript - Extract a link from inside an html code

Can I extract a formatted string from a PDF File name in JavaScript?

How to extract a particular link from a site if it has two links one which I want and other which I don't want?

How can I extract words from a file from a string

Php | Given html string, how can I extract a src attribute?

How can I extract an image from a particular time of a video in Xubuntu 16.04?

How to extract a particular string from the given string in nodejs

How can I extract the string value using regex in javascript?

How can I extract the link to the image here?

How to extract content of html tags from a string using javascript or angularjs?

Extract particular string from stream