使用Javascript解析URL [英] Parse URL with Javascript
问题描述
我正在尝试解析页面的网址。例如,当前页面是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屋!