1

I'm parsing some HTML, and I need to get all html in the body tag. My target string will always look something like this:

<body><div><img src="" />text etc</div></body>

However, I just need:

<div><img src="" />text etc</div>

My target string will always begin and end with those body tags. However, there is the repeated warning of not use Regex to parse HTML, but I do not have any viable solutions for that available, besides Regex at the moment.

Question: Are there any safe Regex(s) to use in this case? Or should I just forget it?

2
  • What actually does "I do not have any viable solutions for that available, besides Regex at the moment" mean? Why don't you have viable solutions? Wouldn't it be best to get viable solutions instead of using an unviable one that you know to be unviable? May 19, 2013 at 0:02
  • You sound like my logic and rhetoric teacher. But yes, you are right. The solution I'm using at the moment is the problem, but it is the best one I have now. The DOM Parser I'm using returns the innerHTML of elements with the element's own tags intact, meaning it returns the element as a whole, not just the HTML inside. I need Regex to extract the inner from that.
    – mattsven
    May 19, 2013 at 0:04

2 Answers 2

3

You didn't show us what your regex is, but it's not as safe as using DOM parsing if it's as simple as:

<body>(.*?)</body>

...because it's possible that </body> is contained in an attribute string or comment. If you're willing to take that risk, then you'll be fine. There's no reason you shouldn't be able to use DOM parsing and just get the text of the body, though, except it would probably be less efficient.

You could also skip the regex and just find the string indices of <body> and </body> and get the substring between them. That should be even faster.

By the way, this is not parsing the HTML; you're just extracting from the HTML

7
  • guess that's the regex way of doing it, but would be much better to use the dom no?
    – Dory Zidon
    May 18, 2013 at 23:53
  • Haha, that was my exact Regex. Now I realize it's not very robust...any suggestions?
    – mattsven
    May 18, 2013 at 23:54
  • @mattcurtis is it likely that </body> will exist in attributes or comments? Or do you have to handle that case? If so, just use a DOM parser. I'm sure that iphone sdk has one, although I'm not familiar with it May 18, 2013 at 23:56
  • It does have a parser, but the problem is that it is giving me that string that includes the <body> tag. I think I might need to try to look between the first > and the last < somehow.
    – mattsven
    May 18, 2013 at 23:57
  • @mattcurtis it would be the equivalent of whatever innerHTML is in JavaScript/other DOM implementations May 18, 2013 at 23:58
0

It's fine to use a RegEx in this case. Having said that there are much easier ways to get the innerHTML of the body tag.

 alert(document.body.innerHTML); 

should give you exactly that with no RegEx... or if you're using jQuery

$(body).html();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.