I would like to split an attribute
at the last occurrence of a character, then add a string and join the array back together. Here is a simplified demo.
In the demo I would like to split the src
attribute at the last occurrence of the .
and then add -fx
to the src
path.
original src
attributes
src="extension.jpg"
src="ext.ension.jpg"
what I am hoping to get
src="extension-fx.jpg"
src="ext.ension-fx.jpg"
To be more specific, the issue is that if I split('.')
and the path has multiple .
problems arise (-fx
not added 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">
To split a string on the last occurrence of a substring:, use the lastIndexOf() method to get the last index of the substring and call the slice() method on the string to get the portions before and after the substring you want to split on.
To split a JavaScript string only on the first occurrence of a character, call the slice() method on the string, passing it the index of the character + 1 as a parameter. The slice method will return the portion of the string after the first occurrence of the character.
Use the str. rsplit() method with maxsplit set to 1 to split a string and get the last element. The rsplit() method splits from the right and will only perform a single split when maxsplit is set to 1 .
To split a string with specific character as delimiter in Java, call split() method on the string object, and pass the specific character as argument to the split() method. The method returns a String Array with the splits as elements in the array.
You can use .attr( attributeName, function )
with callback function to update the attribute value of respective element. To add the string -fx
in the src attribute, String#lastIndexOf
and String#substring
can be used.
// 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); });
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" />
Note: The selector used img[src*="-fx.jpg"]
will select all the images whose src
attribute value contains the given string anywhere. To select images whose src
value ends with given string, use $=
selector.
img[src$="-fx.jpg"] ^
If you want to use regex, following regex can be used.
(\.(?:jpe?g|png|gif))$/
Demo
// 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"); });
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" />
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With