JavaScript RegEx for div tags

后端 未结 10 1129
旧巷少年郎
旧巷少年郎 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:21

    This will not be possible with just a regular expression unless the HTML inside that div contains no other divs. Because what will happen with a pattern like Jeremy's is that it will match the first closing div tag, which wouldn't necessarily be the closing tag for the div#LiveArea element.

    If you have control over the source HTML, you could insert a comment that you could use to match on for the correct "closing" location.

    There are other javascript-only options, but they are each very kludgy or hacky

    1. Set the innerHTML of a hidden element equal to this string of content, THEN pull the innerHTML you need using mmattax's solution. But you will probably have to perform the 2nd step here with a timeout to give the browser time to evaluate this new HTML and expose it to the DOM.
    2. Actually parse the content, keeping track of opening/closing divs as you come across them so you will then know when you're at the correct </div> tag.
    0 讨论(0)
  • 2020-12-17 01:21

    Let jQuery do the parsing for you:

    $(page_html).find("#LiveArea").html();
    
    0 讨论(0)
  • 2020-12-17 01:30

    I'm not sure I follow you when you say, "Javascript variable which holds an html page", but If you need to extract the HTML between such a div, you can use the element's innerHTML property.

    
    var e = document.getElementById('LiveArea');
    if(e) alert(e.innerHTML);
    
    
    
    0 讨论(0)
  • 2020-12-17 01:30

    it seems that javascript doesn't support lookbehinds which is very disapointing, that would make this problem so much easier to solve.

    (?<=<div id="LiveArea">).*(?=<\/div>)

    here are some links that might help out tho.

    • matching html tags
    • mimicking lookbehinds in javascript

    although while discussing the issue of nested tags... that would be beyond the abilities of regex to solve so jeremy's solution is the best you can do with regex. and what is more they have to be on a single line... it won't even match if the the contents of the div are on seperate lines because there is no 's' flag for javascript. I think peter has given the answer for this one.

    0 讨论(0)
提交回复
热议问题