I would like to split attribute by the last occurrence of a character, then add a string and concatenate the array back. Here is a simplified demo .
In the demo, I would like to split the src attribute in the last entry . and then add -fx to the src path.
original src attributes
src="extension.jpg" src="ext.ension.jpg"
what i hope to get
src="extension-fx.jpg" src="ext.ension-fx.jpg"
To be more specific, the problem is that if I split('.') And the path has several problems . ( -fx not appended properly).
$('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); });
img { height: 100px; width: 100px; background: red; } img[src*="-fx.jpg"] { background: blue; }
<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">
source share