简体   繁体   English

正则表达式替换img标签的src属性

[英]Regular Expression to replace src attribute of img tag

I need a regular expression that will properly work, the current one I have is breaking. 我需要一个能正常工作的正则表达式,而我当前的正则表达式正在中断。

The goal is 目标是

Normal src for an image is: Image.png 图像的正常src是:Image.png

Using jQuery on hover I dynamically find the src of an image and replace it with ImageName-Dn.png 在悬停时使用jQuery,我动态地找到图像的src并将其替换为ImageName-Dn.png

On hover off it sets it back to ImageName.png 悬停时将其设置回ImageName.png

My current solution: 我当前的解决方案:

$(document).ready(function(){
   $(".myButton").hover(
        function () {
            var s = $(this).attr('src');
            s = s.substring( 0, s.search(/(\.[a-z]+)$/) ) + '-Dn' + s.match(/(\.[a-z]+)$/)[0]; 
            $(this).attr('src', s);
        },
        function () {
            var o = $(this).attr('src');
            o = o.replace(/-Dn\./, '.'); 
            $(this).attr('src', o);
        }

    );
});

However for some reason the image at some point gets set to ImageName-Dn.png and then screws up and gets set to ImageName-Dn-Dn.png and so on and so forth. 但是由于某种原因,图像在某个时候被设置为ImageName-Dn.png ,然后拧紧并被设置为ImageName-Dn-Dn.png ,依此类推。 Any Help? 有帮助吗?

A quick fix is to test if the string doesn't already have -Dn in it: 一个快速的解决方案是测试字符串中是否没有-Dn:

if (!string.match(/-Dn\./))

Also, with the regexes, you don't need to manually split the string and do multiple searches. 同样,使用正则表达式,您无需手动拆分字符串并进行多次搜索。 You can use grouping to receive what you need in a single replace instruction such as: 您可以使用分组在单个替换指令中接收所需信息,例如:

string.replace(/(.*)\.(.*)/, "$1-Dn.$2")

If you want to read up on regular expressions for Javascript: http://en.wikibooks.org/wiki/JavaScript/Regular_Expressions 如果您想阅读Java的正则表达式: http//en.wikibooks.org/wiki/JavaScript/Regular_Expressions

are you doing this for a mouseover effect? 您这样做是为了获得鼠标悬停效果吗? Why not use image sprites? 为什么不使用图像精灵? Effectively, you just need to create 1 image that contains both version of the image side by side and set it to the background of an element which will display it. 实际上,您只需要创建1张包含并排图像的两个版本的图像,并将其设置为将显示该图像的元素的背景即可。

for example, a 10x10 image and it's mouseover version will become a 10x20 image with the original on top of the mouseover version. 例如,一个10x10图像及其鼠标悬停版本将变为10x20图像,并且原始图片位于鼠标悬停版本的顶部。

you can then create a 10x10 div with the background-image set to the 10x20 image. 然后,您可以创建一个10x10的div,将背景图像设置为10x20的图像。 Since only the top 10x10 will be displayed, you only see the original version. 由于只会显示前10x10,因此您只能看到原始版本。

Then in javascript you can simply attach to an event a call to 然后,在javascript中,您只需将调用

 $(el).style.backgroundPosition = '0px -10px';

on the hover event and 在悬停事件上

$(el).style.backgroundPosition = '0px 0px';

to reset it 重置它

This will shift the background up on the mouse over. 这将使鼠标上的背景向上移动。 Not only is this cleaner than having to deal with regex for a simple image swap, it also reduces the number of files the page has to load. 这不仅比使用正则表达式进行简单的图像交换更清洁,而且还减少了页面必须加载的文件数量。

Hope this helps! 希望这可以帮助!

   function () {
        var s = $(this).attr('src');
        if( !s.match(/-Dn\.[a-z]+$/) ) {
          s = s.substring( 0, s.search(/(\.[a-z]+)$/) ) + '-Dn' + s.match(/(\.[a-z]+)$/)[0]; 
          $(this).attr('src', s);
        }
    },
    function () {
        var o = $(this).attr('src');
        o = o.replace(/-Dn\./, '.'); 
        $(this).attr('src', o);
    }

(added conditional) (添加条件)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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