如果我的网址如下: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]
将给出页码。
关于javascript - 使用正则表达式检查 URL 哈希以查找部分和页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24120552/