duckmike
duckmike

Reputation: 1036

Javascript regular expressions

I have this XML:

<[Results]>
    <[Data]>
        <[div]>THIS IS HTML! <[/div]>
    <[/Data]>
<[/Results]>

What is the regular expression to get <[div]>THIS IS HTML!<[/div]>?

Upvotes: 0

Views: 113

Answers (4)

Madara&#39;s Ghost
Madara&#39;s Ghost

Reputation: 175088

Try this:

<\[div\]>.+?<\[\/div\]>

Will match anything inside the div tags.

Though I am complied to tell you that that regex is NOT perfect. If you want to parse XML, you should use an XML parser.

Do read this post on the subject thoroughly.

Upvotes: 0

hugomg
hugomg

Reputation: 69964

If you can convert this to actual XML, instead of a string, you could use the getElementsByTagName method to find all div tags and the innerHTML(?) property (or innerText/textContent depending on what you want)

Upvotes: 0

Austin Yun
Austin Yun

Reputation: 489

http://www.codinghorror.com/blog/2009/11/parsing-html-the-cthulhu-way.html

Do not parse XML with regexes. Do not.

Upvotes: 2

Adilson de Almeida Jr
Adilson de Almeida Jr

Reputation: 2755

You should avoid catch <´s in the body if you have 2 or more DIVs. Try this:

<[div]>[^<]<[/div]>

Upvotes: -1

Related Questions