Within my variable htmlContent
, there lies a string filled with properly formatted HTML code, which includes various img
tags.
The goal is to extract each value from the src
attribute of these images and place them all in an array named srcList
.
The issue I am encountering with the current code is that it consistently only displays an array consisting of one source value, contrary to my intentions of incorporating all the source URLs into the array instead of just one.
let srcList = [],
imgSrc,
regex = /<img.*src="(.*?)"/gi;
while ((imgSrc = regex.exec(htmlContent)) !== null) {
srcList.push(imgSrc[1]);
}
alert(JSON.stringify(srcList));
How can I adjust this code to function as desired?