3 ms·
Btw it does some weird escaping for JavaScript string literals, also the regex was wrong: const htmlString = '<img src="image1.jpg" alt="Image 1"><img src="i
by weird-eye-issue 4y ago
Btw it does some weird escaping for JavaScript string literals, also the regex was wrong:
const htmlString = '<img src="image1.jpg" alt="Image 1"><img src="image2.jpg">';
const regex = /<img\s[^>]*?src\s*=\s*['"]([^'"]+?)['"][^>]*?(?:alt\s*=\s*['"]([^'"]*?)['"])?[^>]*?>/gi;
let match;
while ((match = regex.exec(htmlString)) !== null) {
const src = match[1];
const alt = match[2] || null;
console.log(`src: \${src}, alt: \${alt}`);
}