Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Split at last occurrence of character then join

Tags:

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">
like image 573
justinw Avatar asked Jan 07 '16 05:01

justinw


People also ask

How do you split the last character of a string?

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.

How do you split a string only on the first instance of specified character?

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.

How do you split the last element in Python?

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 .

How do you split a string at a certain character?

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.


1 Answers

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" />
like image 93
Tushar Avatar answered Oct 16 '22 20:10

Tushar