javascript - Replacing images on a page/website that match a pattern -


i need js/jquery (jquery preferred) can replace images on site match specific pattern. on page have images like:

img src="http://a248.e.akamai.net/a/262/9086/10h/origin-d9.scene7.com/is/image/default/**a34567_v1**?wid=570&hei=413&fmt=jpeg&qlt=92,0&resmode=sharp2&op_usm=1.1,0.5,1,0" 

if image src contains "_v1" change character string "_v2". using example above final result be:

img src="http://a248.e.akamai.net/a/262/9086/10h/origin-d9.scene7.com/is/image/default/**a34567_v2**?wid=570&hei=413&fmt=jpeg&qlt=92,0&resmode=sharp2&op_usm=1.1,0.5,1,0" 

this happen multiple times on page. can assist?

thanks!

since didn't provide multiple examples, cannot tell pattern want use. however, created simplified version regular expressions point in right way:

consider html: want change old new.

<img src="/old_a.png" /> <img src="/new_b.png" /> <img src="/old_c.png" /> 

you loop through img tags using jquery this:

$("img").each(function(i, element){     var pattern = /[old]/; //a regular expression you'll have modify needs     var src = $(element).attr("src");     if(pattern.test(src))         $(element).attr("src", src.replace("old", "new")); }); 

jsfiddle

basicly applies regular expression each image element. you'll have create regular expression yourself, however.


Comments

Popular posts from this blog

linux - Does gcc have any options to add version info in ELF binary file? -

android - send complex objects as post php java -

charts - What graph/dashboard product is facebook using in Dashboard: PUE & WUE -