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!!
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).
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