Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8575811
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T19:46:46+00:00 2026-06-11T19:46:46+00:00

Afternoon all, I am trying to write a script that will extract the first

  • 0

Afternoon all,

I am trying to write a script that will extract the first image from an article via its <img src=""/> tags. So if an article has:

<p>Lorem ipsum dolor sit amet, labore et dolore magna aliqua.<img src="example.jpg"/> Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.</p>

I would like to extract the whole image tag, <img src="example.jpg"/>.

I found this regex which extracts just the location of the image:

content_to_extract_from[/img.*?src="(.*?)"/i,1]

produces, “example.jpg”.

Does anyone know a regex that will capture the tags aswell?

Thanks in advance, Andy

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T19:46:47+00:00Added an answer on June 11, 2026 at 7:46 pm

    Using regexes to parse markup is asking for trouble. You can probably write something that mostly works but which breaks on cases you didn’t foresee. For example you can enclose attributes with single quotes instead of double quotes, which your regex won’t handle

    Much more reliable is to use a real parser, such as nokogiri

    html = Nokogiri::HTML.fragment('<p>Lorem ipsum dolor sit amet, labore et dolore magna aliqua.<img src="example.jpg"/> Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.</p>')
    html.css('img').collect(&:to_s) #=> ["<img src=\"example.jpg\">"] 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Good afternoon, I wish to have a script that will look for all files
Afternoon all, Im trying to find all xml tags that contain a particular word
Afternoon all, Im trying to find a jquery content loading script, im currently developing
I have been trying all afternoon to get the jQuery Sifr Plugin ( http://jquery.thewikies.com/sifr/
I have been struggling all afternoon trying to get a simple mailto: tag to
Afternoon all, Using the code below I'm trying to load what is render by
I've been trying to figure out how to do this all afternoon, but can't
Afternoon All, I have a web form that i wish my user to fill
Good Afternoon all , I have downloaded data from a web service and im
Good afternoon all, I have a Java applet that I wish to embed on

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.