regex to return just body of HTML responseText

Discussion in 'Javascript' started by Mel, Jul 30, 2007.

  1. Mel

    Mel Guest

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

    How can i do this from http_request.responseText string ?

    thanks
     
    Mel, Jul 30, 2007
    #1
    1. Advertising

  2. On Jul 30, 9:37 am, Mel <> 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
     
    Peter Michaux, Jul 30, 2007
    #2
    1. Advertising

  3. On Jul 30, 9:41 am, Peter Michaux <> wrote:
    > On Jul 30, 9:37 am, Mel <> 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
     
    Peter Michaux, Jul 30, 2007
    #3
  4. On Jul 30, 10:03 am, Duncan Booth <>
    wrote:
    > Peter Michaux <> wrote:
    > > On Jul 30, 9:41 am, Peter Michaux <> wrote:
    > >> On Jul 30, 9:37 am, Mel <> 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>/
     
    Peter Michaux, Jul 30, 2007
    #4
  5. 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
     
    Thomas 'PointedEars' Lahn, Jul 31, 2007
    #5
  6. 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$>
     
    Thomas 'PointedEars' Lahn, Jul 31, 2007
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. fochie
    Replies:
    9
    Views:
    327
  2. Elizabeth

    ? in XMLHttpRequest.ResponseText

    Elizabeth, Feb 25, 2006, in forum: Javascript
    Replies:
    9
    Views:
    313
    Elizabeth
    Feb 28, 2006
  3. e271828
    Replies:
    3
    Views:
    160
    Martin Honnen
    Aug 25, 2006
  4. Replies:
    6
    Views:
    246
  5. jr
    Replies:
    6
    Views:
    332
Loading...

Share This Page