I am currently utilizing AJAX technology.
My approach involves sending a GET request that retrieves a raw HTML string representing the content of a webpage. I am grappling with the challenge of isolating all elements enclosed between div
tags:
<div role="main" class="main-container js-quickedit-main-content " style="padding:0px;margin:0px;">
<!-- Content to extract lies within this div -->
</div>
In my attempt to achieve this, I experimented with the following regular expression:
var extracted_content = result.match('(<div role="main" class="main-container js-quickedit-main-content " style="padding:0px;margin:0px;">)[^]*(</div>)').toString();
However, this regex does not yield the desired outcome. The issue arises when dealing with nested div
elements. By selecting one specific div
container, it may inadvertently capture its parent's closing tag instead of its own.
Is there an alternative method for accurately extracting the text enclosed within the first occurrence of opening and closing div
tags (<div ..>
and </div>
)?
Edit: Included expected and actual results from the regex operation
The anticipated output should resemble:
[HTML content inside multiple nested divs]
Whereas the current output is:
[HTML content but prematurely terminated due to nesting]