使用 Javascript 获取 URL 的特定部分
我有几个 URL,我需要获取 URI 最后部分的特定部分
如果我有 URL www.test.co/index.php/government-printer-sales.html
我只需要从 URL 获取government
。所有网址都采用相同的结构,因此如果我有 www.test.co/index.php/management-fees.html
我需要获取单词 management
我尝试了以下
var str = document.URL.split('/');
var type = str[5].split('-',1);
给了我一些结果,但我确信有更好的方法。如果有的话我可以从 mootools 或简单的 javascript 获得这个
I have a couple of URL's that I need to obtain a specific part of the last part of the URI
If I have the url www.test.co/index.php/government-printer-sales.html
I only need to obtain government
from the URL. All url's are in the same structure so if I have www.test.co/index.php/management-fees.html
I need to obtain the word management
I tried the following
var str = document.URL.split('/');
var type = str[5].split('-',1);
which gives me some result, but I'm sure there is a better way. If there's anyway I can obtain this from eiter mootools or just plain javascript
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以使用正则表达式来提取最后一个斜杠之后和第一个破折号之前的字符串:
You could use a regular expression to pull out the string after the last slash and before the first dash:
您可以查看此处,然后查看此处,然后检查以下代码:
You can look here and then here and then check this code:
Splice 是一个很棒的函数,传入负数,因为第一个参数将创建一个新数组,其中的元素从数组末尾开始计数。
这和python的列表拼接lst[:-1]类似
Splice is a great function passing in a negative number as the first argument will create a new array with elements counting from the end of the array.
This is similar to python's list splicing lst[:-1]
试试这个:
它搜索
index.php/
后面的任何字符(不包括连字符),但后跟一个连字符并获取之间的值。Try this:
It searches for any characters following
index.php/
excluding a hyphen, but followed by a single hyphen and fetches the value in between.