SmartestVEGA
SmartestVEGA

Reputation: 8889

How to match HTML header and footer

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);

Upvotes: 0

Views: 500

Answers (2)

Lucas Araujo
Lucas Araujo

Reputation: 1688

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

Upvotes: 1

Simon Appelt
Simon Appelt

Reputation: 305

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

should do the trick.

Upvotes: 1

Related Questions