const regex = /(apple)+/gmi;
// Alternative syntax using RegExp constructor
// const regex = new RegExp('(apple)+', 'gmi')
const str = `<div class="mw-parser-output"><div role="note" class="hatnote navigation-not-searchable">This article is about the fruit. For the technology company, see <a href="/wiki/Apple_Inc." title="Apple Inc.">Apple Inc.</a> For other uses, see <a href="/wiki/Apple_(disambiguation)" class="mw-disambig" title="Apple (disambiguation)">Apple (disambiguation)</a>, <a href="/wiki/Apple_Blossom_(disambiguation)" class="mw-redirect mw-disambig" title="Apple Blossom (disambiguation)">Apple Blossom (disambiguation)</a>, and <a href="/wiki/Apple_tree_(disambiguation)" class="mw-disambig" title="Apple tree (disambiguation)">Apple tree (disambiguation)</a>.</div> <p class="mw-empty-elt"> </p> <table class="infobox biota" style="text-align: left; width: 200px; font-size: 100%"> <tbody><tr> <th colspan="2" style="text-align: center; background-color: rgb(180,250,180)">Apple </th></tr> <tr> <td colspan="2" style="text-align: center"><a href="/wiki/File:Honeycrisp.jpg" class="image"><img alt="Honeycrisp.jpg" src="//upload.wikimedia.org/wikipedia/commons/thumb/f/f4/Honeycrisp.jpg/220px-Honeycrisp.jpg" width="220" height="220" srcset="//upload.wikimedia.org/wikipedia/commons/thumb/f/f4/Honeycrisp.jpg/330px-Honeycrisp.jpg 1.5x, //upload.wikimedia.org/wikipedia/commons/thumb/f/f4/Honeycrisp.jpg/440px-Honeycrisp.jpg 2x" data-file-width="2176" data-file-height="2176" /></a> </td></tr> <tr> <td colspan="2" style="text-align: center; font-size: 88%">Fruit </td></tr> <tr> <td colspan="2" style="text-align: center"><a href="/wiki/File:Malus_domestica_a1.jpg" class="image"><img alt="Malus domestica a1.jpg" src="//upload.wikimedia.org/wikipedia/commons/thumb/2/22/Malus_domestica_a1.jpg/220px-Malus_domestica_a1.jpg" width="220" height="164" srcset="//upload.wikimedia.org/wikipedia/commons/thumb/2/22/Malus_domestica_a1.jpg/330px-Malus_domestica_a1.jpg 1.5x, //upload.wikimedia.org/wikipedia/commons/thumb/2/22/Malus_domestica_a1.jpg/440px-Malus_domestica_a1.jpg 2x" data-file-width="1280" data-file-height="956" /></a> </td></tr> <tr> <td colspan="2" style="text-align: center; font-size: 88%">Flowers </td></tr> <tr> <th colspan="2" style="min-width:15em; text-align: center; background-color: rgb(180,250,180)"><a href="/wiki/Taxonomy_(biology)" title="Taxonomy (biology)">Scientific classification</a> <span class="plainlinks" style="font-size:smaller; float:right; padding-right:0.4em; margin-left:-3em;"><a href="/wiki/Template:Taxonomy/Malus" title="e"><img alt="e" src="//upload.wikimedia.org/wikipedia/commons/7/74/Red_Pencil_Icon.png" width="16" height="16" data-file-width="16" data-file-height="16" /></a></span> </th></tr> <tr> <td>Kingdom:</td> <td><a href="/wiki/Plant" title="Plant">Plantae</a> </td></tr> <tr> <td><i>Clade</i>:</td> <td><a href="/wiki/Flowering_plant" title="Flowering plant">Angiosperms</a> </td></tr> <tr> <td><i>Clade</i>:</td> <td><a href="/wiki/Eudicots" title="Eudicots">Eudicots</a> </td></tr> <tr> <td><i>Clade</i>:</td> <td><a href="/wiki/Rosids" title="Rosids">Rosids</a> </td></tr> <tr> <td>Order:</td> <td><a href="/wiki/Rosales" title="Rosales">Rosales</a> </td></tr> <tr> <td>Family:</td> <td><a href="/wiki/Rosaceae" title="Rosaceae">Rosaceae</a> </td></tr> <tr> <td>Genus:</td> <td><i><a href="/wiki/Malus" title="Malus">Malus</a></i> </td></tr> <tr> <td>Species: </td> <td><span class="species"><i><b>M. pumila</b></i></span> </td></tr> <tr> <th colspan="2" style="text-align: center; background-color: rgb(180,250,180)"><a href="/wiki/Binomial_nomenclature" title="Binomial nomenclature">Binomial name</a> </th></tr> <tr> <td colspan="2" style="text-align: center"><b><span class="binomial"><i>Malus pumila</i></span></b><br /><small><a href="/wiki/Philip_Miller" title="Philip Miller">Miller</a>, 1768</small> </td></tr> <tr> <th colspan="2" style="text-align: center; background-color: rgb(180,250,180)"><a href="/wiki/Synonym_(taxonomy)" title="Synonym (taxonomy)">Synonyms</a><sup id="cite_ref-1" class="reference"><a href="#cite_note-1">[1]</a></sup><sup id="cite_ref-2" class="reference"><a href="#cite_note-2">[2]</a></sup> </th></tr> <tr> <td colspan="2" style="text-align: left"> <ul><li><i>Malus communis</i> <small>Desf.</small></li> <li><i>Malus domestica</i> <small>Borkh. nom. illeg.</small></li> <li><i>M. frutescens</i> <small>Medik.</small></li> <li><i>M. paradisiaca</i> <small>(L.) Medikus</small></li> <li><i>M. sylvestris</i> <small>Mil.</small></li> <li><i>Pyrus malus</i> <small>L.</small></li> <li><i>Pyrus malus</i> var. <i>paradisiaca</i> <small>L.</small></li> <li><i>Pyrus dioica</i> <small>Moench</small></li></ul> </td></tr> </tbody></table> <p>An <b>apple</b> is a sweet, edible <a href="/wiki/Fruit" title="Fruit">fruit</a> produced by an <b>apple tree</b> (<i><b>Malus pumila</b></i>). Apple <a href="/wiki/Fruit_tree" title="Fruit tree">trees</a> are <a href="/wiki/Agriculture" title="Agriculture">cultivated</a> worldwide, and are the most widely grown species in the <a href="/wiki/Genus" title="Genus">genus</a> <i><a href="/wiki/Malus" title="Malus">Malus</a></i>. The tree originated in <a href="/wiki/Central_Asia" title="Central Asia">Central Asia</a>, where its wild ancestor, <i><a href="/wiki/Malus_sieversii" title="Malus sieversii">Malus sieversii</a></i>, is still found today. Apples have been grown for thousands of years in <a href="/wiki/Asia" title="Asia">Asia</a> and <a href="/wiki/Europe" title="Europe">Europe</a>, and were brought to North America by <a href="/wiki/European_colonization_of_the_Americas" title="European colonization of the Americas">European colonists</a>. Apples have religious and <a href="/wiki/Mythology" title="Mythology">mythological</a> significance in many cultures, including <a href="/wiki/Norse_mythology" title="Norse mythology">Norse</a>, <a href="/wiki/Greek_mythology" title="Greek mythology">Greek</a> and <a href="/wiki/Christianity_in_Europe" title="Christianity in Europe">European Christian traditions</a>. </p><p>Apple trees are large if grown from seed. Generally apple <a href="/wiki/Cultivar" title="Cultivar">cultivars</a> are propagated by <a href="/wiki/Grafting" title="Grafting">grafting</a> onto rootstocks, which control the size of the resulting tree. There are more than 7,500 known <a href="/wiki/List_of_apple_cultivars" title="List of apple cultivars">cultivars of apples</a>, resulting in a range of desired characteristics. Different cultivars are bred for various tastes and uses, including <a href="/wiki/Cooking_apple" title="Cooking apple">cooking</a>, eating raw and <a href="/wiki/Apple_cider" title="Apple cider">cider</a> production. Trees and fruit are prone to a number of <a href="/wiki/Fungus" title="Fungus">fungal</a>, bacterial and pest problems, which can be controlled by a number of <a href="/wiki/Organic_farming" title="Organic farming">organic</a> and non-organic means. In 2010, the fruit's <a href="/wiki/Genome" title="Genome">genome</a> was <a href="/wiki/DNA_sequencing" title="DNA sequencing">sequenced</a> as part of research on disease control and selective breeding in apple production. </p><p> Worldwide production of apples in 2014 was 84.6 million <a href="/wiki/Tonnes" class="mw-redirect" title="Tonnes">tonnes</a>, with China accounting for 48% of the total.<sup id="cite_ref-faostat_3-0" class="reference"><a href="#cite_note-faostat-3">[3]</a></sup></p><div class="mw-references-wrap"><ol class="references"> <li id="cite_note-1"><span class="mw-cite-backlink"><b><a href="#cite_ref-1">^</a></b></span> <span class="reference-text"><cite id="CITEREFElizabeth_E._Dickson2015" class="citation">Elizabeth E. Dickson (2015), <a rel="nofollow" class="external text" href="http://www.efloras.org/florataxon.aspx?flora_id=1&taxon_id=200010913">"<i>Malus pumila</i> Miller, Gard. Dict. ed. 8. Malus no. 3. 1768"</a>, <a rel="nofollow" class="external text" href="http://www.efloras.org/florataxon.aspx?flora_id=1&taxon_id=200010913"><i>Flora of North America</i></a>, <b>9</b>, <a rel="nofollow" class="external text" href="https://web.archive.org/web/20150725131538/http://www.efloras.org/florataxon.aspx?flora_id=1&taxon_id=200010913">archived</a> from the original on 25 July 2015</cite><span title="ctx_ver=Z39.88-2004&rft_val_fmt=info%3Aofi%2Ffmt%3Akev%3Amtx%3Abook&rft.genre=bookitem&rft.atitle=Malus+pumila+Miller%2C+Gard.+Dict.+ed.+8.+Malus+no.+3.+1768&rft.btitle=Flora+of+North+America&rft.date=2015&rft.au=Elizabeth+E.+Dickson&rft_id=http%3A%2F%2Fwww.efloras.org%2Fflorataxon.aspx%3Fflora_id%3D1%26taxon_id%3D200010913&rfr_id=info%3Asid%2Fen.wikipedia.org%3AApple" class="Z3988"><span style="display:none;"> </span></span></span> </li> <li id="cite_note-2"><span class="mw-cite-backlink"><b><a href="#cite_ref-2">^</a></b></span> <span class="reference-text"><cite id="CITEREFWendy_L._Applequist2014" class="citation">Wendy L. Applequist (2014), "Report of the Nomenclature Committee for Vascular Plants: 66: (1933). To conserve <i>Malus domestica</i> Borkh. against <i>M. pumila</i> Miller, <i>M. communis</i> Desf., <i>M. frutescens</i> Medik., and <i>Pyrus dioica</i> Moench (Rosaceae). Proposed by G.-Z. Qian, L.-F. Liu & G.-G. Tang in Taxon 59(2): 650–652. 2010. Votes: 6–11–1 (not recommended)", <i>Taxon</i>, <b>63</b> (6): 1358–1371, <a href="/wiki/Digital_object_identifier" title="Digital object identifier">doi</a>:<a rel="nofollow" class="external text" href="//doi.org/10.12705/636.20">10.12705/636.20</a></cite><span title="ctx_ver=Z39.88-2004&rft_val_fmt=info%3Aofi%2Ffmt%3Akev%3Amtx%3Ajournal&rft.genre=article&rft.jtitle=Taxon&rft.atitle=Report+of+the+Nomenclature+Committee+for+Vascular+Plants%3A+66%3A+%281933%29.+To+conserve+Malus+domestica+Borkh.+against+M.+pumila+Miller%2C+M.+communis+Desf.%2C+M.+frutescens+Medik.%2C+and+Pyrus+dioica+Moench+%28Rosaceae%29.+Proposed+by+G.-Z.+Qian%2C+L.-F.+Liu+%26+G.-G.+Tang+in+Taxon+59%282%29%3A+650%E2%80%93652.+2010.+Votes%3A+6%E2%80%9311%E2%80%931+%28not+recommended%29&rft.volume=63&rft.issue=6&rft.pages=1358-1371&rft.date=2014&rft_id=info%3Adoi%2F10.12705%2F636.20&rft.au=Wendy+L.+Applequist&rfr_id=info%3Asid%2Fen.wikipedia.org%3AApple" class="Z3988"><span style="display:none;"> </span></span></span> </li> <li id="cite_note-faostat-3"><span class="mw-cite-backlink"><b><a href="#cite_ref-faostat_3-0">^</a></b></span> <span class="error mw-ext-cite-error" lang="en" dir="ltr">Cite error: The named reference <code>faostat</code> was invoked but never defined (see the <a href="/wiki/Help:Cite_errors/Cite_error_references_no_text" title="Help:Cite errors/Cite error references no text">help page</a>). </span></li> </ol></div> <!-- NewPP limit report Parsed by mw1227 Cached time: 20180711070656 Cache expiry: 1900800 Dynamic content: false CPU time usage: 0.484 seconds Real time usage: 0.605 seconds Preprocessor visited node count: 5917/1000000 Preprocessor generated node count: 0/1500000 Post‐expand include size: 23760/2097152 bytes Template argument size: 6939/2097152 bytes Highest expansion depth: 19/40 Expensive parser function count: 4/500 Unstrip recursion depth: 0/20 Unstrip post‐expand size: 267/5000000 bytes Number of Wikibase entities loaded: 0/400 Lua time usage: 0.310/10.000 seconds Lua memory usage: 4.01 MB/50 MB --> <!-- Transclusion expansion time report (%,ms,calls,template) 100.00% 570.551 1 -total 61.03% 348.232 1 Template:Speciesbox 59.06% 336.978 1 Template:Taxobox/core 28.16% 160.656 1 Template:Taxobox/taxonomy 16.99% 96.915 74 Template:Taxon_info 9.77% 55.733 2 Template:Citation 8.75% 49.901 7 Template:Taxon_link 7.68% 43.833 1 Template:About 6.99% 39.896 1 Template:Pp-semi-indef 6.65% 37.930 15 Template:Delink --> </div>`;
// 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