检查URL散列以使用正则表达式查找节和页面

Inspect URL hash to find section and page using regex

本文关键字:查找 正则表达式 URL 检查      更新时间:2023-09-26

如果我有一个URL: domain.com/#/section-1/page-2/

我想检查一下,看看是哪个章节和页码。

我试过:

var currentSection = 0;
var currentPage = 0;
if( window.location.hash != '' ) {
    currentSection = window.location.hash.match('[section-/]');
    currentPage = window.location.hash.match('[page-/]');
}

但是我的正则表达式相当差。我怎样才能得到我想要的信息?

location.href上使用一个简单的正则表达式加上一些映射:

var nums = location.href.match(/(section|page)-'d+/g).map(
    function(x){ return +x.replace(/'D/g,"") }
);

nums[0]将给出截面号。

nums[1]给出页码