# Regex in HTML

**URL:** <https://www.ruby-forum.com/t/regex-in-html/94171>\
**Category:** Rails\
**Created:** [April 24, 2006, 3:09am UTC](https://www.ruby-forum.com/t/regex-in-html/94171 "2006-04-24T03:09:01Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Hampton](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/8edcca/32.png) [@Hampton](https://www.ruby-forum.com/u/Hampton)\
**Post date:** [April 24, 2006, 3:09am UTC](https://www.ruby-forum.com/t/regex-in-html/94171/1 "2006-04-24T03:09:01Z")

</div>

So, I’m trying to write a nice bit of regex to handle finding anchor  
tags in  
a bit of html.

This is what I’ve got…

/\<[aA][^\>]_\>[^\<]_\</[aA]\>/

I’m planning on using this with a gsub!.

Here is what it has to do…

[Anything in here.](http://stuff.com)

As you can see, the regex I have will indeed work for this instance. It  
will  
match with ‘[Anything in  
here.](http://stuff.com)’

Except, here is where it fails…

[**Anything** in here.](http://stuff.com)

I still need ‘[**Anything** in here.](http://stuff.com)’

This regex doesn’t work…  
/\<[aA][^\>]_\>._\</[aA]\>/  
Because my middle tag will just keep going and return

[ **Anything** in  
here.](http://stuff.com)

because it doesn’t have any reason to stop.

Any suggestions?

-hampton.

---

<div class="post-metadata">

**Author:** ![Hampton](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/8edcca/32.png) [@Hampton](https://www.ruby-forum.com/u/Hampton)\
**Post date:** [April 24, 2006, 3:30am UTC](https://www.ruby-forum.com/t/regex-in-html/94171/2 "2006-04-24T03:30:23Z")

</div>

replace .\* with [^(\</a)(\</A)]

That should work.

-N

---

<div class="post-metadata">

**Author:** ![Hampton](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/8edcca/32.png) [@Hampton](https://www.ruby-forum.com/u/Hampton)\
**Post date:** [April 24, 2006, 3:36am UTC](https://www.ruby-forum.com/t/regex-in-html/94171/3 "2006-04-24T03:36:29Z")

</div>

You could use the ? non-greedy modifier for this:

/\<a.\*?\</a\>/

This will match from the beginning of the \<a to the first .

On 4/23/06, Hampton [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I still need '\<a href=“[http://stuff.com](http://stuff.com)”

–  
Family management on rails: [http://www.famundo.com](http://www.famundo.com) - coming soon!  
My development related blog: [http://devblog.famundo.com](http://devblog.famundo.com)

---

<div class="post-metadata">

**Author:** ![Hampton](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/8edcca/32.png) [@Hampton](https://www.ruby-forum.com/u/Hampton)\
**Post date:** [April 24, 2006, 4:03am UTC](https://www.ruby-forum.com/t/regex-in-html/94171/4 "2006-04-24T04:03:42Z")

</div>

WOooooowhoooo!!!

Thanks!
