$(function() { $("a img.swap").mouseover(function() { this.src = this.src.replace(/^(.+)(\.[a-z]+)$/, "$1_o$2"); }); $("a img.swap").mouseout(function() { this.src = this.src.replace(/^(.+)_o(\.[a-z]+)$/, "$1$2"); }); $("a img.swap").each(function () { this.preloaded = new Image; this.preloaded.src = this.src.replace(/^(.+)(\.[a-z]+)$/, "$1_o$2"); }); });