I have a string
that contains code HTML
with tags <img>
in it, I need to create an array with the src of each img
but I can not tell you by numbers where to cut, because I do not know where the tag appears img
or where it ends
<p>Hola, esto es un ejemplo<img src="./img-uploads/345753e8-bba6-d75f-3f0f-0f4d0b9bd493.jpg" style="width: 757px;"> más texto de prueba<img src="./img-uploads/48deeb1c-a32e-cc28-76b4-e59806c7b1d5.jpg" style="width: 50px;"><br></p>
My idea is to find as start <img src="
and as final, the next character in order that would be "
, because what this code generates is an editor and the added style will always vary.
I solved it in a slightly weird way but it works to find in any string a piece using as index pieces of string
that surround it
Thanks to Lois6b for your help , what I'm looking for right now is the correct answer, however, I leave the code in case someone wants to do something similar without being able to use getElementsByTagName.
var string = '<p>Hola, esto es un ejemplo<img src="./img-uploads/345753e8-bba6-d75f-3f0f-0f4d0b9bd493.jpg" style="width: 757px;"> más texto de prueba<img src="./img-uploads/48deeb1c-a32e-cc28-76b4-e59806c7b1d5.jpg" style="width: 50px;"><br></p>'
var resultado = [];
var ultimo = -1;
do {
var n = string.indexOf('<img src="',ultimo+1); // img
ultimo=n;
var m = string.indexOf('"',ultimo+10) // comillas
if(n!=-1)
resultado.push(string.slice(n+10, m)); //+10 por el tamaño de <img src=
ultimo=n;
} while (n!=-1);
console.log(resultado);