简体   繁体   中英

javascript replacing strings

I have a small custom made thumbnail gallery. On click of a thumbnail I want to get the bigger version of the image. To do this I am getting the img 's src . With that I want to replace a certain section of the string for another string. For example the thumbnail's source may look like this:

/media/uploads/headshots/width_60_259850_10150280251835435_717615434_9570015_4084479_n.jpg

I need to replace width_60_ with width_104_ . Is this possible with Javascript?

Simply use the JS replace() function :

var imgSrc = "/media/uploads/headshots/width_60_259850_10150280251835435_717615434_9570015_4084479_n.jpg"
var newSrc = imgSrc.replace("width_60_", "width_104_");

Here's a working jsFiddle .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM