const regex = new RegExp('<loc>[^>]*\\K\\d{6}(?=/</loc>)', 'gm')
const str = `<?xml version="1.0" encoding="utf-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<url>
<loc>https://somelink.com/category/building-materials/concrete/hand-tools/</loc>
<lastmod>2022-09-11T02:10:42+02:00</lastmod>
<changefreq>weekly</changefreq>
<priority>0.8</priority>
</url>
<url>
<loc>https://somelink.com/category/building-materials/concrete/hand-tools/screws/screws-145890/</loc>
<lastmod>2022-09-11T02:11:06+02:00</lastmod>
<changefreq>weekly</changefreq>
<priority>0.8</priority>
</url>
<url>
<loc>https://somelink.com/category/building-materials/concrete/hand-tools/screws/screws-145489/</loc>
<lastmod>2022-09-11T02:11:14+02:00</lastmod>
<changefreq>weekly</changefreq>
<priority>0.8</priority>
</url>
<url>
<loc>https://somelink.com/category/building-materials/concrete/hand-tools/hammer/hammer-145488/</loc>
<lastmod>2022-09-11T02:10:42+02:00</lastmod>
<changefreq>weekly</changefreq>
<priority>0.8</priority>
</url>
<url>
<loc>https://somelink.com/category/inside/heating/floor-heating/pert-222-010274/</loc>
<lastmod>2022-09-11T02:11:06+02:00</lastmod>
<changefreq>weekly</changefreq>
<priority>0.8</priority>
</url>
<url>
<loc>https://somelink.com/category/building-materials/paint/</loc>
<lastmod>2022-09-11T02:11:14+02:00</lastmod>
<changefreq>weekly</changefreq>
<priority>0.8</priority>
</url>
<url>
<loc>https://somelink.com/category/building-materials/screws-and-nails/</loc>
<lastmod>2022-09-11T02:10:42+02:00</lastmod>
<changefreq>weekly</changefreq>
<priority>0.8</priority>
</url>
<url>
<loc>https://somelink.com/category/building-materials/concrete/power-toools/</loc>
<lastmod>2022-09-11T02:11:06+02:00</lastmod>
<changefreq>weekly</changefreq>
<priority>0.8</priority>
</url>
<url>
<loc>https://somelink.com/category/inside/heating/floor-heating/pert-182-010272/</loc>
<lastmod>2022-09-11T02:11:14+02:00</lastmod>
<changefreq>weekly</changefreq>
<priority>0.8</priority>
</url>
<url>
<loc>https://somelink.com/category/inside/heating/floor-heating/pert-202-010273/</loc>
<lastmod>2022-09-11T02:10:42+02:00</lastmod>
<changefreq>weekly</changefreq>
<priority>0.8</priority>
</url>
<url>
<loc>https://somelink.com/category/inside/bathroom/</loc>
<lastmod>2022-09-11T02:11:06+02:00</lastmod>
<changefreq>weekly</changefreq>
<priority>0.8</priority>
</url>
<url>
<loc>https://somelink.com/category/inside/pipes/draining-pipes-168544/</loc>
<lastmod>2022-09-11T02:11:14+02:00</lastmod>
<changefreq>weekly</changefreq>
<priority>0.8</priority>
</url>
</xml>`;
// 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