const regex = /<a\s+href="https?:\/\/?(?:www\.)?((?:(?!-)[A-Za-z0-9-]{1,63}(?<!-)\.)+[A-Za-z]{2,8})(?:"|\/).*?>(?:https?:\/\/)?(?:www\.)?\1/img;
// Alternative syntax using RegExp constructor
// const regex = new RegExp('<a\\s+href="https?:\\\/\\\/?(?:www\\.)?((?:(?!-)[A-Za-z0-9-]{1,63}(?<!-)\\.)+[A-Za-z]{2,8})(?:"|\\\/).*?>(?:https?:\\\/\\\/)?(?:www\\.)?\\1', 'img')
const str = `<span style="color:rgb(128,0,0);font-weight:bold">> Оформлен заказ в магазине <a href="http://www.test1.com" target="_blank">test1.com</a> </spant-family:arial,sans-serif>
<a href="http://test.site.com/11222/sdf.html">http://www.test.site.com</a><br style="color:rgb(0,0,0);font-family:arial,sans-serif;font-size:12.8px"><span style="color:rgb(0,0,0);font-family:arial,sans-serif;font-size:12.8px">SOME MESSAGE TEXT</span><br style="color:rgb(0,0,0);font-family:arial,sans-serif;font-size:12.8px"><a href="http://www.test2.com/">www.test2.com/123/test</a>
SOME TEXT</span><br style="color:rgb(0,0,0);font-family:arial,sans-serif;font-size:12.8px"><a href="http://www.TEsst.com/">https://tesst.com</a><br style="color:rgb(0,0,0);font-family:arial,sans-serif;font-size:12.8px"><span style="color:rgb(0,0,0);font-family:arial,sans-serif;font-size:12.8px"> === =size:12.8px"><span style="color:rgb(0,0,0);font-family:arial,sans-serif;font-size:12.8px"><a href="http://site.org">site.org</a>
`;
// 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