使用 Javascript 解析 URL [英] Parse URL with Javascript

查看:34
本文介绍了使用 Javascript 解析 URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能的重复:
如何将 URL 解析为主机名和 javascript 中的路径?

我正在尝试解析页面的 url.例如当前页面是location.href.

I’m trying to parse the url of the page. For example the current page is location.href.

所以我博客的主页,如果我使用 alert(location.href);它将在警告框中返回http://diaryofthedead.co.cc/".如果我使用 alert(location.href);在我博客的第二页,它将返回http://diaryofthedead.co.cc/page/2"在警告框中.有什么方法可以解析URL以获取最后的数字.有谁知道我怎么能做到这一点?我可以使用通配符或其他东西来做类似的事情:location.href+"page/"+*;而*等于page/"后面的任何内容,然后将*变成一个变量?

So the main page of my blog, if I use alert(location.href); it will return "http://diaryofthedead.co.cc/" in an alert box. If I use alert(location.href); on page two of my blog, it will return "http://diaryofthedead.co.cc/page/2" in an alert box. Is there any way to parse the URL to get the number at the end. Does anyone know how I could do that? Could I use wildcard or something, to do something like: location.href+"page/"+*; While * is equal to whatever follows "page/", and then turn * into a variable?

推荐答案

可以使用

var pagenum = location.pathname.match(//page/(.*)/)[1];

它会提取您网址中/page/"之后的任何内容;

It will extract anything past '/page/' in your URL;

这篇关于使用 Javascript 解析 URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆