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 3492088
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T11:42:58+00:00 2026-05-18T11:42:58+00:00

I’m trying to select some text using regular expressions leaving all img tags intact.

  • 0

I’m trying to select some text using regular expressions leaving all img tags intact.

I’ve found the following code that selects all img tags:

/<img[^>]+>/g

but actually having a text like:

This is an untagged text.
<p>this is my paragraph text</p>
<img src="http://example.com/image.png" alt=""/>
<a href="http://example.com/">this is a link</a>

using the code above will select the img tag only

/<img[^>]+>/g #--> using this code will result in:
<img src="http://example.com/image.png" alt=""/>

but I would like to use some regex that select everything but the image like:

/magical regex/g # --> results in:
This is an untagged text.
<p>this is my paragraph text</p>
<a href="http://example.com/">this is a link</a>

I’ve also found this code:

/<(?!img)[^>]+>/g

which selects all tags except the img one. but in some cases I will have untagged text or text between tags so this won’t work for my case. 🙁

is there any way to do it?
Sorry but I’m really new to regular expressions so I’m really struggling for few days trying to make it work but I can’t.

Thanks in advance


UPDATE:

Ok so for the ones thinking I would like to parse it, sorry I don’t want it, I just want to select text.

Another thing, I’m not using any language in specific, I’m using Yahoo Pipes which only provide regex and some string tools to accomplish the job. but it doesn’t evolves any programming code.

for better understanding here is the way regex module works in yahoo pipes:

http://pipes.yahoo.com/pipes/docs?doc=operators#Regex


UPDATE 2

Fortuntately I’m being able to strip the text near the img tag but on a step-by-step basis as @Blixt recommended, like:

<(?!img)[^>]+> , replace with "" #-> strips out every tag that is not img
(?s)^[^<]*(.*), replace with $1  #-> removes all the text before the img tag
(?s)^([^>]+>).*, replace with $1 #-> removed all the text after the img tag

the problem with this is that it will only catch the first img tag and then I would have to do it manually and catch the others hard-coding it, so I still not sure if this is the best solution.

  • 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-05-18T11:42:58+00:00Added an answer on May 18, 2026 at 11:42 am

    The regexp you have to find the image tags can be used with a replace to get what you want.

    Assuming you are using PHP:

    $htmlWithoutIMG = preg_replace('/<img[^>]+>/g', '', $html);
    

    If you are using Javascript:

    var htmlWithoutIMG = html.replace(/<img[^>]+>/g, '');
    

    This takes your text, finds the <img> tags and replaces them with nothing, ie. it deletes them from the text, leaving what you want. Can not recall if the <,> need escaping.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

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.