I ha got these types of hrefs
我有这些类型的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".
现在我需要编写一个函数,它将把这个作为输入,并返回“tourand - safaris-bookenquiry -form”。
Please suggest!!
请建议! !
2 个解决方案
#1
8
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:
你必须注意这样格式化的url:
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).
…也就是说,那些带有前斜杠的元素,因为它们将在末尾创建一个空数组元素(这可能会影响解析)。您可能想要首先清除数组中的空元素,然后再检索您真正想要的特定字符串(使用类似的解决方案)。
#2
3
If your string will always be of that exact form (i.e. always have the same number of /
characters before the part you want) then you can do this:
如果你的字符串总是那样的形式(例如,在你想要的部分之前总是有相同数量的/字符),那么你可以这样做:
var partYouWant = yourString.split("/")[5];
split
does what it says, and splits the string, using the specified string as a delimiter (in this case, it uses the /
character as the delimiter). That returns an array, and the part you want is at index 5.
split执行它所说的操作,并使用指定的字符串作为分隔符对字符串进行分割(在本例中,它使用/字符作为分隔符)。返回一个数组,你想要的部分在索引5处。
Note that jQuery will not help you in the slightest here. It's plain old JavaScript that you want.
注意,jQuery在这里对您没有任何帮助。您需要的是普通的旧JavaScript。
#1
8
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:
你必须注意这样格式化的url:
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).
…也就是说,那些带有前斜杠的元素,因为它们将在末尾创建一个空数组元素(这可能会影响解析)。您可能想要首先清除数组中的空元素,然后再检索您真正想要的特定字符串(使用类似的解决方案)。
#2
3
If your string will always be of that exact form (i.e. always have the same number of /
characters before the part you want) then you can do this:
如果你的字符串总是那样的形式(例如,在你想要的部分之前总是有相同数量的/字符),那么你可以这样做:
var partYouWant = yourString.split("/")[5];
split
does what it says, and splits the string, using the specified string as a delimiter (in this case, it uses the /
character as the delimiter). That returns an array, and the part you want is at index 5.
split执行它所说的操作,并使用指定的字符串作为分隔符对字符串进行分割(在本例中,它使用/字符作为分隔符)。返回一个数组,你想要的部分在索引5处。
Note that jQuery will not help you in the slightest here. It's plain old JavaScript that you want.
注意,jQuery在这里对您没有任何帮助。您需要的是普通的旧JavaScript。