JavaScript lazy regex for matching HTML tags

Posted by Grnbeagle on Stack Overflow See other posts from Stack Overflow or by Grnbeagle
Published on 2010-06-17T05:43:43Z Indexed on 2010/06/17 5:53 UTC
Read the original article Hit count: 297

Filed under:
|
|

Hi,

I'm having a problem writing a regular expression for matching HTML tags. I found a similar entry here, but this didn't quite work in my case.

Here's my test string:

<div id="div0" class="myclass">here's some text
that may include whitespace</div><div id="div1" class="myclass">
and some more here
</div>

And here's my regex based on the aforementioned entry:

<div[^>]*class="myclass">[^~]*?<\/div>

Note that I need to match the first instance of <div /> with class of "myclass." The content may have carriage returns. These <div> tags won't be nested.

Here's a rubular page for testing: http://rubular.com/r/vlfcikKMXk

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about html