Im looking to split a body of html into an array.
Here is an example of what the code looks like:
<p><h2 class="title">Title 1</h2></p>
<p>Section 1: Lorem ipsum dolor sit amet, consectetur adipisicing elit.</p>
<p>velit saepe ducimus aspernatur, quam quaerat autem. Consectetur, vitae.</p>
<p><h2 class="title">Title 2</h2></p>
<p>Section 2: Lorem ipsum dolor sit amet, consectetur adipisicing elit.</p>
<p><h2 class="title">Title 3</h2></p>
<p>Section 3: Lorem ipsum dolor sit amet, consectetur adipisicing elit.</p>
Basically I'd like to split the sections up using a positive lookbehind with the following pattern <p><h2 class="title">*</h2></p>
or any other type of regex pattern.
Essentionally I'm looking to have an array that contains something like so...
<p><h2 class="title">Title 1</h2></p>
<p>Section 1: Lorem ipsum dolor sit amet, consectetur adipisicing elit.</p>
<p>velit saepe ducimus aspernatur, quam quaerat autem. Consectetur, vitae.</p>
<p><h2 class="title">Title 2</h2></p>
<p>Section 2: Lorem ipsum dolor sit amet, consectetur adipisicing elit.</p>
<p><h2 class="title">Title 3</h2></p>
<p>Section 3: Lorem ipsum dolor sit amet, consectetur adipisicing elit.</p>
This is the code that will alway be constant <p><h2 class="title">*</h2></p>
. The content will always be encapsulate within <p>
tags.
Here is an example of the script Im parsing the data through...
$(contentArr).each(function(ele, idx){
var content = ele, contentTrun;
var contentRegex = /(<p>.*<\/p>)/im;
var matchContent = contentRegex.exec(content);
//parse block to get it ready for styling and effect
var contentRegex = /((?!<p><h2 class="title".*?\n)<p>.*<\/p>)/igm;
var parsedContent = content.replace(contentRegex, "$1");
//insert parsed content into html block
$("pressBlocks").insert("<div class=\"blockContentOutter\">\
<span class=\"accordionText\">... <a class=\"readMore\">Read More</a></span>\
<div class=\"blockContent\">"+parsedContent+"</div>\
</div>");
});