# Img (regular expressions

**URL:** <https://www.ruby-forum.com/t/img-regular-expressions/146446>\
**Category:** Ruby\
**Created:** [August 21, 2008, 8:32am UTC](https://www.ruby-forum.com/t/img-regular-expressions/146446 "2008-08-21T08:32:37Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![newb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/2acd7d/32.png) [@newb](https://www.ruby-forum.com/u/newb)\
**Post date:** [August 21, 2008, 8:32am UTC](https://www.ruby-forum.com/t/img-regular-expressions/146446/1 "2008-08-21T08:32:37Z")

</div>

Hi All…  
I Need to Extract Img tag Using Regular Expressions From The Html Page  
\<\s_img [^\>]src\s=\s_(["’])(.\*?)\1  
Is This Code Would be ok  
if So How it can Be Implemented?  
Any Ideas??

---

<div class="post-metadata">

**Author:** ![newb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/2acd7d/32.png) [@newb](https://www.ruby-forum.com/u/newb)\
**Post date:** [August 21, 2008, 9:49am UTC](https://www.ruby-forum.com/t/img-regular-expressions/146446/2 "2008-08-21T09:49:51Z")

</div>

2008/8/21 Newb N. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> I Need to Extract Img tag Using Regular Expressions From The Html Page  
> \<\s_img [^\>]src\s=\s_(["'])(.\*?)\1  
> Is This Code Would be ok

I would choose a different regexp.

> if So How it can Be Implemented?

What exactly?

> Any Ideas??

[http://code.whytheluckystiff.net/hpricot/](http://code.whytheluckystiff.net/hpricot/)

Cheers

robert

---

<div class="post-metadata">

**Author:** ![newb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/2acd7d/32.png) [@newb](https://www.ruby-forum.com/u/newb)\
**Post date:** [August 21, 2008, 10:19am UTC](https://www.ruby-forum.com/t/img-regular-expressions/146446/3 "2008-08-21T10:19:09Z")

</div>

Newb N. wrote:

> I Need to Extract Img tag Using Regular Expressions From The Html Page  
> \<\s_img [^\>]src\s=\s_(["’])(.\*?)\1  
> Is This Code Would be ok  
> if So How it can Be Implemented?  
> Any Ideas??

Regexp is not a parser; it strongly resists matching well-formed syntax,  
such as  
HTML.

You need to write unit tests so you can “see” what you are doing. They  
will feed  
samples of input to your parser, and assert the output contains no \<img  
tags.

I would load these strings into libxml-ruby or Hpricot documents, then  
use XPath  
to seek ‘//img’, then delete their nodes from the document, then write  
the  
documents back. But note HTML supports several other ways to inject  
images,  
including CSS styles, tags, etc.

You need to consult with your client how clean you need your HTML. If  
they say  
to only allow _, , **, or tags, for example, you could  
use XPath  
to seek ‘//\*’, meaning all nodes, then replace their tag names with  
 ,  
delete all their attributes, and write the document back.**_

Next, there might be gems out there to do this (or plugins), so you  
could google  
for [rails scrub html], to just find one, and either raid its source, or  
install  
and use it.

---

<div class="post-metadata">

**Author:** ![newb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/2acd7d/32.png) [@newb](https://www.ruby-forum.com/u/newb)\
**Post date:** [August 21, 2008, 10:20am UTC](https://www.ruby-forum.com/t/img-regular-expressions/146446/4 "2008-08-21T10:20:54Z")

</div>

> You need to consult with your client how clean you need your HTML. If  
> they say to only allow _, , **, or tags, for example,  
> you could use XPath to seek ‘//\*’, meaning all nodes, then replace their  
> tag names with , delete all their attributes, and write the  
> document back.**_

Another way to scrub input is don’t allow raw HTML. Only allow a wiki  
markup,  
such as RedCloth. Some wikis allow ‘‘italic’’ and ‘’‘bold’’’ content,  
and very  
little else. Then you don’t need to scrub it; you simply let the wiki  
engine  
convert it to harmless read-only HTML.
