1
votes

I would like to replace with javascript all image paths from a string with the img tag. Would I need a reg exp for this or a script ?

Image name and ext can be different in my string. In other words I have different images.

Replace ../IMG/epi.gif with

<img src="img/epi.gif">

initial string

"This image ../IMG/epi.gif and this image ../IMG/secondImage.jpg are not good"

needed result will be

'This image <img src="img/epi.gif"/> and this image <img src="img/secondImage.jpg"/> are not good'
2
Is the lowercasing of the image path intentional?Jørgen R
Yes It is and also the ../ is missingACe
By the way all images start with ../IMG and they end with the extention .gif or .jpg ...ACe

2 Answers

1
votes

Try this:

update

var img = 'This image ../IMG/epi.gif and this image ../IMG/secondImage.jpg are not good';
var newImg = img.replace(/\.\.\/IMG\/([^\.]+)(\.jpg|\.gif)/gi, '<img src="img/$1$2">');
alert (newImg);
0
votes

var img = '../IMG/xEPI_STOPPED_TOT.gif';
var newImg = img.replace(/\.\.\/IMG\/(.*)/, '<img src="img/$1">');
alert (newImg);