JavaScript RegEx for div tags

后端 未结 10 1128
旧巷少年郎
旧巷少年郎 2020-12-17 00:26

I have a JavaScript variable which holds an HTML page and due to the setup I need to extract everything between

and
相关标签:
10条回答
  • 2020-12-17 01:07
    var temp = document.createElement('DIV');
    temp.innerHTML = YourVariable;
    var liveArea;
    for (var i = 0; i < temp.childNodes.length; i++)
    {
       if (temp.childNodes[i].id == 'LiveArea')
       {
           liveArea = temp.childNodes[i];
           break;
       }
    }
    
    0 讨论(0)
  • 2020-12-17 01:07

    I found this article surfing on the web which take a DIV id and shows it on a new page to print it;

    function getPrint(print_area)
    {
    //Creating new page
    var pp = window.open();
    //Adding HTML opening tag with <HEAD> … </HEAD> portion 
    pp.document.writeln('<HTML><HEAD><title>Print Preview</title>')
    pp.document.writeln('<LINK href=Styles.css type="text/css" rel="stylesheet">')
    pp.document.writeln('<LINK href=PrintStyle.css ' + 
                        'type="text/css" rel="stylesheet" media="print">')
    pp.document.writeln('<base target="_self"></HEAD>')
    
    //Adding Body Tag
    pp.document.writeln('<body MS_POSITIONING="GridLayout" bottomMargin="0"');
    pp.document.writeln(' leftMargin="0" topMargin="0" rightMargin="0">');
    //Adding form Tag
    pp.document.writeln('<form method="post">');
    
    //Creating two buttons Print and Close within a HTML table
    pp.document.writeln('<TABLE width=100%><TR><TD></TD></TR><TR><TD align=right>');
    pp.document.writeln('<INPUT ID="PRINT" type="button" value="Print" ');
    pp.document.writeln('onclick="javascript:location.reload(true);window.print();">');
    pp.document.writeln('<INPUT ID="CLOSE" type="button" ' + 
                        'value="Close" onclick="window.close();">');
    pp.document.writeln('</TD></TR><TR><TD></TD></TR></TABLE>');
    
    //Writing print area of the calling page
    pp.document.writeln(document.getElementById(print_area).innerHTML);
    //Ending Tag of </form>, </body> and </HTML>
    pp.document.writeln('</form></body></HTML>'); 
    

    }

    You will call this script sending the DIV id you want to get;

    btnGet.Attributes.Add("Onclick", "getPrint('YOURDIV');")
    

    It worked exactly as I wanted. Hope it helps

    0 讨论(0)
  • 2020-12-17 01:08

    Use the following regular expression:

    <div id="[^"]*">(.*?)</div>
    
    0 讨论(0)
  • 2020-12-17 01:10

    Sorry for late reply, if someone else stumbles on this problem here is my suggestion, assuming you have access to the page you are reading from source code.

    Add a HTML-comment like this

    <div id="LiveArea">
    <!--LiveArea-->
    Content here
    <!--EndLiveArea-->
    </div>
    

    Then match it with

    htmlVal.match(/<\!\-\-LiveArea"\-\->(.*?)<\!\-\-EndLiveArea"\-\->/);
    
    0 讨论(0)
  • 2020-12-17 01:12
    var html = "<stuff><div id=\"LiveArea\">hello stackoverflow!</div></stuff>";
    
    var matches = html.match(/<div\s+id="LiveArea">[\S\s]*?<\/div>/gi);
    var matches = matches[0].replace(/(<\/?[^>]+>)/gi, ''); // Strip HTML tags?
    
    alert(matches);
    
    0 讨论(0)
  • 2020-12-17 01:12

    This should do it:

    pattern = /<div id="LiveArea">(.*?)<\/div>/;
    matches = your_html_var.match(pattern);
    the_string = matches[0];
    
    document.write(the_string);
    
    0 讨论(0)
提交回复
热议问题