在最后一次出现角色时拆分然后加入

jus*_*inw 9 javascript css regex jquery

我想attribute在最后一个字符处拆分,然后添加一个字符串并将数组重新连接在一起.这是一个简化的演示.

在演示中,我想src在最后一次出现时拆分属性,.然后添加-fxsrc路径中.

原始src属性

src="extension.jpg" src="ext.ension.jpg"

我希望得到什么

src="extension-fx.jpg" src="ext.ension-fx.jpg"

更具体地说,问题是如果我split('.')和路径出现多个.问题(-fx没有正确添加).

$('img').each(function(){
	var a = $(this).attr('src');
    var b = a.split('.')
    var c = b[0] + '-fx' + '.' + b[1];
    console.log(c);
    $(this).attr('src', c);    
});
Run Code Online (Sandbox Code Playgroud)
img {
    height: 100px;
    width: 100px;
    background: red;
}

img[src*="-fx.jpg"] {
    background: blue;
}
Run Code Online (Sandbox Code Playgroud)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<img src="extension.jpg">
<img src="ext.ension.jpg">
Run Code Online (Sandbox Code Playgroud)

Tus*_*har 17

您可以使用.attr( attributeName, function )回调函数来更新各个元素的属性值.要添加字符串-fxsrc属性,String#lastIndexOf并且String#substring可以使用.

// Get the src attribute value of image one by one
$('img').attr('src', function(i, src) {
  // Get the index of the last .
  var lastIndex = src.lastIndexOf('.');

  // Add the string before the last .
  // Return updated string, this will update the src attribute value
  return src.substr(0, lastIndex) + '-fx' + src.substr(lastIndex);
});
Run Code Online (Sandbox Code Playgroud)
img {
  height: 100px;
  width: 100px;
  background: red;
}
img[src$="-fx.jpg"] {
  background: blue;
}
Run Code Online (Sandbox Code Playgroud)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<img src="extension.jpg" />
<img src="ext.ension.jpg" />
Run Code Online (Sandbox Code Playgroud)

注意:使用的选择器img[src*="-fx.jpg"]将选择src属性值包含给定字符串的所有图像.要选择src值以给定字符串结尾的图像,请使用$=选择器.

img[src$="-fx.jpg"]
       ^
Run Code Online (Sandbox Code Playgroud)

如果要使用正则表达式,可以使用以下正则表达式.

(\.(?:jpe?g|png|gif))$/
Run Code Online (Sandbox Code Playgroud)

演示

// Get the src attribute value of image one by one
$('img').attr('src', function(i, src) {
  return src.replace(/(\.(?:jpe?g|png|gif))$/, "-fx$1");
});
Run Code Online (Sandbox Code Playgroud)
img {
  height: 100px;
  width: 100px;
  background: red;
}
img[src$="-fx.jpg"] {
  background: blue;
}
Run Code Online (Sandbox Code Playgroud)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<img src="extension.jpg" />
<img src="ext.ension.jpg" />
Run Code Online (Sandbox Code Playgroud)


Abh*_*nav 7

这是通过lastIndexOf在javascript中使用和'substring'函数来实现的.我刚刚更新了你的小提琴.看看它

lastIndexOf- >将获取角色的位置,.然后使用子串函数,您可以加入以获得所需的结果

$('img').each(function(){
    var a = $(this).attr('src');
    var pos = a.lastIndexOf('.');
    var newchar = a.substring(0,pos)+'-fx';
    var replacingchar = newchar+a.substr(pos);
    console.log(replacingchar);

});
Run Code Online (Sandbox Code Playgroud)

JS FIDDLE


Ada*_*ett 5

您可以在最后一次出现 . 和:

split = str.match(/(.*)\.(.*)/)
Run Code Online (Sandbox Code Playgroud)

如果实际上至少有一个.(在 RegExp 中表示为\.),则结果将是一个数组,其中元素 2 是最后一个之前的所有内容.,元素 3 是它之后的所有内容。