我有这些类型的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
Run Code Online (Sandbox Code Playgroud)
现在我需要编写一个函数,将其作为输入,并返回"tours-and-safaris-booking-inquiry-form".
请建议!!
你可以做一个基本的分裂(到一个数组)并得到倒数第二个元素.
var _array = url.split('/'),
_foo = _array[_array.length-2];
Run Code Online (Sandbox Code Playgroud)
您必须注意以这种方式格式化的URL:
http://your.domain.com/path/to/something/
Run Code Online (Sandbox Code Playgroud)
...也就是那些带有正斜杠的那些,因为那些会在末尾创建一个空的数组元素(这会抛弃你的解析).您可能希望首先通过删除空元素来清理数组,然后再检索您真正想要的特定字符串(使用类似此解决方案的方法).