kiran Gopal kiran Gopal - 5 months ago 14
jQuery Question

regular expression to add a folder to all the image src

Hi need to find a regular expression that need to add a extra folder to all the image src. For example assets/img/case-study/flyconnect/1.jpg to be changed to assets/img/case-study/flyconnect/big/1.jpg
Can any one help me on this ?

"<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>"

Answer

Using regular expressions \/([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;});

In your specific case

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;});