Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > regex to return just body of HTML responseText

Reply
Thread Tools

regex to return just body of HTML responseText

 
 
Mel
Guest
Posts: n/a
 
      07-30-2007
I just need the content between <body> and </body> of a message

How can i do this from http_request.responseText string ?

thanks

 
Reply With Quote
 
 
 
 
Peter Michaux
Guest
Posts: n/a
 
      07-30-2007
On Jul 30, 9:37 am, Mel <(E-Mail Removed)> wrote:
> 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

 
Reply With Quote
 
 
 
 
Peter Michaux
Guest
Posts: n/a
 
      07-30-2007
On Jul 30, 9:41 am, Peter Michaux <(E-Mail Removed)> wrote:
> On Jul 30, 9:37 am, Mel <(E-Mail Removed)> wrote:
>
> > 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


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

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

Peter

 
Reply With Quote
 
Peter Michaux
Guest
Posts: n/a
 
      07-30-2007
On Jul 30, 10:03 am, Duncan Booth <(E-Mail Removed)>
wrote:
> Peter Michaux <(E-Mail Removed)> wrote:
> > On Jul 30, 9:41 am, Peter Michaux <(E-Mail Removed)> wrote:
> >> On Jul 30, 9:37 am, Mel <(E-Mail Removed)> wrote:

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

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

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

>
> 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>/



 
Reply With Quote
 
Thomas 'PointedEars' Lahn
Guest
Posts: n/a
 
      07-31-2007
Peter Michaux wrote:
> [...] 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
 
Reply With Quote
 
Thomas 'PointedEars' Lahn
Guest
Posts: n/a
 
      07-31-2007
Peter Michaux wrote:
> [...] 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
--
Prototype.js was written by people who don't know javascript for people
who don't know javascript. People who don't know javascript are not the
best source of advice on designing systems that use javascript.
-- Richard Cornford, <f806at$ail$1$(E-Mail Removed)>
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
AJAX function doesn't return responseText in the right place jr Javascript 6 08-12-2010 02:06 PM
How make regex that means "contains regex#1 but NOT regex#2" ?? seberino@spawar.navy.mil Python 3 07-01-2008 03:06 PM
html snippet returned by xmlhttprequest.responseText displayed as text mihirnmehta@gmail.com Javascript 6 12-14-2006 03:50 AM
? in XMLHttpRequest.ResponseText Elizabeth Javascript 9 02-28-2006 02:58 AM
xmlhttp problem using Mozilla ... responseText is always null fochie Javascript 9 05-26-2005 07:42 AM



Advertisments