I am currently working on updating a specific string using the .replace() method. Interestingly, while the first and last occurrences are successfully replaced, the second one remains unchanged.
var text = "![alt text](https://www.reduceimages.com/img/image-after3.jpg) adadw hjagwdjh ![alt text](https://www.reduceimages.com/img/image-after2.jpg) akjhwdhawk ![alt text](https://www.reduceimages.com/img/image-afte1r.jpg)";
console.log("Final Result --- > " + formatText(text));
function formatText(content) {
const regex = /!\[(.*?)\]\((.*?)\)/g;
let m;
var printResult = (array) => {
content = content.replace(array[0], "<img src='" + 123 + "'>");
return false;
};
while ((m = regex.exec(content))) {
if (m.index === regex.lastIndex) {
regex.lastIndex++;
}
printResult(m);
console.log("After Replace " + content + "\n");
}
return content;
}