使用 Javascript 获取 URL 的特定部分

发布于 2024-12-08 08:11:34 字数 436 浏览 0 评论 0原文

我有几个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

·深蓝 2024-12-15 08:11:34

您可以使用正则表达式来提取最后一个斜杠之后和第一个破折号之前的字符串:

var regex = /\/([^/\-]+)[^/]*$/;
var matches = regex.exec('www.test.co/index.php/government-printer-sales.html');
var type = matches[1];  // government

You could use a regular expression to pull out the string after the last slash and before the first dash:

var regex = /\/([^/\-]+)[^/]*$/;
var matches = regex.exec('www.test.co/index.php/government-printer-sales.html');
var type = matches[1];  // government
仄言 2024-12-15 08:11:34

您可以查看此处,然后查看此处,然后检查以下代码:

var myString = "www.test.co/index.php/government-printer-sales.html";
var myRegexp = /(www.test.co\/index.php\/)(\w+)-(.)*/g;
var match = myRegexp.exec(myString);
alert(match);

You can look here and then here and then check this code:

var myString = "www.test.co/index.php/government-printer-sales.html";
var myRegexp = /(www.test.co\/index.php\/)(\w+)-(.)*/g;
var match = myRegexp.exec(myString);
alert(match);
明媚殇 2024-12-15 08:11:34

Splice 是一个很棒的函数,传入负数,因为第一个参数将创建一个新数组,其中的元素从数组末尾开始计数。

document.URL
> "http://stackoverflow.com/questions/7671441/getting-a-specific-part-of-a-url-using-javascript"

document.URL.split('/').splice(-1)[0].split('-')[0]
> "getting"

这和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.

document.URL
> "http://stackoverflow.com/questions/7671441/getting-a-specific-part-of-a-url-using-javascript"

document.URL.split('/').splice(-1)[0].split('-')[0]
> "getting"

This is similar to python's list splicing lst[:-1]

看轻我的陪伴 2024-12-15 08:11:34

试试这个:

var type = window.location.pathname.match(/index.php\/([^-]+)(?:-)/)[1];

它搜索 index.php/ 后面的任何字符(不包括连字符),但后跟一个连字符并获取之间的值。

Try this:

var type = window.location.pathname.match(/index.php\/([^-]+)(?:-)/)[1];

It searches for any characters following index.php/ excluding a hyphen, but followed by a single hyphen and fetches the value in between.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文