-2

I have several anchor elements like the following and need to extract the full URL of the contained image in a for loop.

<a href="https://genesis28.test/?attachment_id=1524"><img src="https://genesis28.test/wp-content/uploads/2019/01/alesia-kazantceva-283288-1024x683.jpg" alt="" data-id="1524" data-link="https://genesis28.test/?attachment_id=1524" class="wp-image-1524" srcset="https://genesis28.test/wp-content/uploads/2019/01/alesia-kazantceva-283288-1024x683.jpg 1024w, https://genesis28.test/wp-content/uploads/2019/01/alesia-kazantceva-283288-300x200.jpg 300w, https://genesis28.test/wp-content/uploads/2019/01/alesia-kazantceva-283288-768x512.jpg 768w, https://genesis28.test/wp-content/uploads/2019/01/alesia-kazantceva-283288-600x400.jpg 600w, https://genesis28.test/wp-content/uploads/2019/01/alesia-kazantceva-283288.jpg 1600w" sizes="(max-width: 1024px) 100vw, 1024px"></a>

In the above example, I want to get https://genesis28.test/wp-content/uploads/2019/01/alesia-kazantceva-283288.jpg. In other words, the URL that is to the left of 1600w.

Can anyone help with how to obtain the URLs?

Sridhar Katakam
  • 1,206
  • 13
  • 53
  • What have you tried? Seems like a simple trask to get the src and then just split it on the `-`. – j08691 Jan 21 '19 at 21:12

1 Answers1

-1

In jQuery:

var imgUrls = $("img").attr('src');

will give you an array of all the URLS for image tags.

You cannot use Regex to reliably parse HTML because HTML is a non-regular language and regular expressions are for parsing (you guessed it,) regular languages. See this infamous stackoverflow answer for a more in-depth explanation: RegEx match open tags except XHTML self-contained tags

Our_Benefactors
  • 3,220
  • 3
  • 21
  • 27