Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get value before last forward slash using Jquery

Tags:

jquery

I ha got these types of hrefs

http://localhost:8080/en/reservations/tours-and-safaris-booking-enquiry-form/dubai/index.aspx
http://localhost:8080/en/reservations/tours-and-safaris-booking-enquiry-form/muscat/index.aspx

Now I need to write a function which will take this as input and will return "tours-and-safaris-booking-enquiry-form".

Please suggest!!

like image 499
Manoj Singh Avatar asked Sep 29 '11 10:09

Manoj Singh


1 Answers

You can do a rudimentary split (to an array) and get the second-to-the-last element.

var _array = url.split('/'),
    _foo = _array[_array.length-2];

You have to watch out for URLs formatted this way:

http://your.domain.com/path/to/something/

... that is, those with trailing forward slashes, as those will create an empty array element at the end (which can throw off your parsing). You may want to sanitize your array first by removing empty elements before retrieving the particular string you really want (using something like this solution).

like image 95
Richard Neil Ilagan Avatar answered Nov 25 '22 23:11

Richard Neil Ilagan