Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
122
rated 0 times [  129] [ 7]  / answers: 1 / hits: 23506  / 12 Years ago, thu, april 12, 2012, 12:00:00

I'm trying to do something which seems fairly basic, but can't seem to get it working.



I'm trying to strip the characters after the last instance of an underscore.



I have this string, for example:



www/images/10/20120412/28-696-b0b9815463e47c9371b02b7202788a75_tn.jpg



and I'm trying to strip out the 'tn.jpg' to produce:



www/images/10/20120412/28-696-b0b9815463e47c9371b02b7202788a75_



I tried doing .slice(0,-6) but that will only work for instances of _tn.jpg and not _med.jpg.



Ultimately, I'm going to be swapping in different sizes of images (_med.jpg, _full.jpg etc.) and it needs to be only after the last underscore (there might be underscores in the URL).



Any help would be greatly appreciated!



Zack


More From » replace

 Answers
6

You can it like this:



var testURL = dvuivnhuiv_ew;
var output = testURL.substring(0, testURL.lastIndexOf('_') + 1);
console.log(output);

[#86281] Wednesday, April 11, 2012, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
andreguym

Total Points: 125
Total Questions: 112
Total Answers: 103

Location: Wallis and Futuna
Member since Tue, Mar 30, 2021
3 Years ago
;