const regex = /(\s*<script.*>)((\s|.)*)(<\/script\s*?>)/g;
// Alternative syntax using RegExp constructor
// const regex = new RegExp('(\\s*<script.*>)((\\s|.)*)(<\\\/script\\s*?>)', 'g')
const str = `<script type="text/template" data-category="text" data-img-src="section-01.jpg" data-section-name="1">
<div class="zpsection" data-element-type="section">
<div class="zpcontainer">
<div class="zprow" data-element-type="row" data-column-ratio="12,12">
<div class="zpcol-md-12 zpcol-sm-12" data-element-type="column">
<div class="zpelement zpelem-heading" data-element-type="heading">
<h2 class="zpheading zpheading-align-center" data-editor="true">The face of the moon was in shadow</h2>
</div>
<div class="zpelement zpelem-text" data-element-type="text">
<div class="zptext zptext-align-center" data-editor="true">
<p>Almost before we knew it, we had left the ground. All their equipment and instruments are alive.Mist enveloped the ship three hours out from port. The spectacle before us was indeed sublime.A red flair silhouetted the jagged edge of a wing.</p>
</div>
</div>
<div class="zpelement zpelem-button" data-element-type="button">
<div class="zpbutton-container zpbutton-align-center"> <a href="javascript:;" class="zpbutton-wrapper zpbutton zpbutton-type-primary zpbutton-size-md"> <span class="zpbutton-content">Get Started Now</span> </a> </div>
</div>
</div>
</div>
</div>
</div>
</script>
`;
// Reset `lastIndex` if this regex is defined globally
// regex.lastIndex = 0;
let m;
while ((m = regex.exec(str)) !== null) {
// This is necessary to avoid infinite loops with zero-width matches
if (m.index === regex.lastIndex) {
regex.lastIndex++;
}
// The result can be accessed through the `m`-variable.
m.forEach((match, groupIndex) => {
console.log(`Found match, group ${groupIndex}: ${match}`);
});
}
Please keep in mind that these code samples are automatically generated and are not guaranteed to work. If you find any syntax errors, feel free to submit a bug report. For a full regex reference for JavaScript, please visit: https://developer.mozilla.org/en/docs/Web/JavaScript/Guide/Regular_Expressions