正则表达式将文件夹添加到所有图像src

时间:2016-07-04 09:04:37

标签: javascript jquery regex

您需要找到一个需要在所有图像src中添加额外文件夹的正则表达式。例如,资产/ img / case-study / flyconnect / 1.jpg将更改为assets / img / case-study / flyconnect / big / 1.jpg 任何人都可以帮助我吗?

"<div class="image-bg"><img id="airlineImg" src="assets/img/case-study/flyconnect/1.jpg"></div><span class="zoomEnl">Enlarge</span> <div class="thumpnail-wrap"><div class="thumbnailimages"><div class="pull-left active"><a href="#" data-target="#airlineImg" data-imgg="assets/img/case-study/flyconnect/1.jpg"><img class="img-responsive" src="assets/img/case-study/flyconnect/1.jpg"></a></div><div class="pull-left"><a href="#" data-target="#airlineImg" data-imgg="assets/img/case-study/flyconnect/2.jpg"><img class="img-responsive" src="assets/img/case-study/flyconnect/2.jpg"></a></div><div class="pull-left"><a href="#" data-target="#airlineImg" data-imgg="assets/img/case-study/flyconnect/3.jpg"><img class="img-responsive" src="assets/img/case-study/flyconnect/3.jpg"></a></div></div></div>"

1 个答案:

答案 0 :(得分:1)

使用正则表达式\/([A-Za-z0-9\s_-]+\.[A-Za-z0-9]+)

str.replace(/\/([A-Za-z0-9\s_-]+\.[A-Za-z0-9]+)/gi, function myFunction(x){return "/big" + x;});

根据您的具体情况

var str = document.getElementById("airlineImg").getAttribute("src");
str.replace(/\/([A-Za-z0-9\s_-]+\.[A-Za-z0-9]+)/gi, function myFunction(x){return "/big" + x;});