0

I have the following script, where I parse entire HTML and bind to a div (Homepage):

function htmlResponse() {
    var res = "";
    debugger;
    $.ajaxPrefilter(function (options, originalOptions, jqXHR) {
        options.async = true;
    });
    $.ajax({
        url: "http://localhost:1469/Home.aspx",
        success: function (result) {
            res = result;
            res = res.replace(/\/CMSPages\/GetResource\.ashx/g, 'http://localhost:1469/CMSPages/GetResource.ashx');
            res = res.replace(/\/WebResource\.axd/g, 'http://localhost:1469/WebResource.axd');
            res = res.replace('<ul>', '<ul class="fallback browse-experience-drop-down-width">');

            $("#homePage").html(res);
        }
    }).done(function () {

    });
};

res variable contains full HTML which needs to be binds to div homepage.

How I can write another regex which will fetch only <HEADER> till </HEADER> and <FOOTER> till </FOOTER>?

And bind those two sections in two div tags?

Something like below:

header = res.regex("REGEX FOR FETCHING HEADER");
footer = res.regex("REGEX FOR FETCHING FOOTER");

                $("#HEADER").html(header);
                $("#FOOTER").html(footer);
locknies
  • 1,345
  • 3
  • 15
  • 36
SmartestVEGA
  • 8,415
  • 26
  • 86
  • 139
  • 3
    [Don't parse HTML with regex!](http://stackoverflow.com/a/1732454/418066) – Biffen Apr 18 '16 at 08:06
  • i know, but we have a situation here, because we are trying to integrate a CMS website to external website, which cms wont supports integration to external sites, hence need to parse and change the source and load – SmartestVEGA Apr 18 '16 at 08:08
  • 1
    @SmartestVEGA — So *parse* it. You're already using jQuery which will do that for you. – Quentin Apr 18 '16 at 08:09
  • how i do that, can you give some reference, i checked it in internet and didn't got any relevant one – SmartestVEGA Apr 18 '16 at 08:11
  • AFAIK, it should get parsed automatically unless the CMS is sticking out the wrong content-type, in which case you can force it with `dataType: "html"`. – Quentin Apr 18 '16 at 08:17

2 Answers2

1
/<HEADER>(\n.*)*<\/HEADER>/g

should do the trick.

Simon Appelt
  • 305
  • 4
  • 12
1

You can try this.

For header:

/<(?:HEADER|header)>([\s\S]*?)<\/(?:HEADER|header)>/g

Regex Demo

For footer:

/<(?:footer|FOOTER)>([\s\S]*?)<\/(?:footer|FOOTER)>/g

Regex Demo

Lucas Araujo
  • 1,648
  • 16
  • 25