将 ../IMG/xEPI_STOPPED_TOT.gif 替换为 <img src="">

Replace ../IMG/xEPI_STOPPED_TOT.gif with <img src="">

我想用 javascript 替换带有 img 标签的字符串中的所有图像路径。我需要这个或脚本的 reg exp 吗?

图像名称和扩展名在我的字符串中可以不同。换句话说,我有不同的图像。

将 ../IMG/epi.gif 替换为

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

初始字符串

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

需要的结果是

'This image <img src="img/epi.gif"/> and this image <img src="img/secondImage.jpg"/> are not good'

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

试试这个:

更新

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/">');
alert (newImg);