拉出部分页面URL

发布于 2025-01-03 11:39:16 字数 395 浏览 3 评论 0原文

我的页面 URL 如下所示:

http://www.mydomain.com/directory/thisismy_page.html

使用 JavaScript,我想提取页面文件名中下划线之前的部分。所以在这个例子中,我想要:

thisismy

我目前有这个:

var filename = location.pathname.substring(location.pathname.lastIndexOf("/") + 1);

但这会拉出 thisismy_page.html。如何在下划线处“停止”它,以便我得到的只是 thisismy

My page's URL is like this:

http://www.mydomain.com/directory/thisismy_page.html

Using JavaScript, I want to pull out the part of the page's filename before the underscore. So in this example, I want:

thisismy

I currently have this:

var filename = location.pathname.substring(location.pathname.lastIndexOf("/") + 1);

But that pulls out thisismy_page.html. How do I "stop" it at the underscore so all I get is thisismy?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

晨光如昨 2025-01-10 11:39:16
filename=filename.split("_")[0];

这会在下划线处将其拆分并获取第一个(或第 0 部分)。

filename=filename.split("_")[0];

This splits it at the underscore and takes the first (or 0th part).

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