regex to return just body of HTML responseText

M

Mel

I just need the content between <body> and </body> of a message

How can i do this from http_request.responseText string ?

thanks
 
P

Peter Michaux

I just need the content between <body> and </body> of a message

You need it or want to set it?
How can i do this from http_request.responseText string ?

If you need it in the http_request.reponseText string (ie that string
is/has some JavaScript) then just use document.body.innerHTML.

To set it

document.body.innerHTML = http_request.responseText;

Peter
 
P

Peter Michaux

You need it or want to set it?


If you need it in the http_request.reponseText string (ie that string
is/has some JavaScript) then just use document.body.innerHTML.

To set it

document.body.innerHTML = http_request.responseText;

Peter

I didn't see the subject line of your post at first. Won't this work?

/<body>(.)</body>/

Peter
 
P

Peter Michaux

You mean apart from the unescaped slash in the middle of the regex, the
fact that a dot only matches 1 character, and also that a dot won't match a
newline?

Of course :) I suppose I was trying to figure out what is abnormal or
tricky about this RegExp question.

Peter
This would probably work most of the time:

/<body[^>]*>((?:.|\n)*)<\/body>/
 
T

Thomas 'PointedEars' Lahn

Peter said:
[...] Won't this work?

/<body>(.)</body>/

If the infix slash would be escaped, this would work if the body element
was a one-liner with one-non-whitespace-character content :)

You were looking for

req.responseText.match(/<body(?:\s+(?:.|\n)*?)?>((.|\n)*)<\/body>/)[1]

or a backwards-compatible substitute like

req.responseText.match(/<body(\s+[^>]*)?>((.|\n)*)<\/body>/)[2]


PointedEars
--
var bugRiddenCrashPronePieceOfJunk = (
navigator.userAgent.indexOf('MSIE 5') != -1
&&
navigator.userAgent.indexOf('Mac') != -1
) // Plone, register_function.js:16
 
T

Thomas 'PointedEars' Lahn

Peter said:
[...] Won't this work?

/<body>(.)</body>/

If the infix slash would be escaped, this would work if the body element
was a one-liner with one-non-whitespace-character content :)

You were looking for

req.responseText.match(/<body(?:\s+(?:.|\s)*?)?>((.|\s)*)<\/body>/)[1]

or a backwards-compatible substitute like

req.responseText.match(/<body(\s+[^>]*)?>((.|\s)*)<\/body>/)[2]


PointedEars
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top