我需要在此代码中编写正则表达式来检测网址上的ID,如下所示:http://example.com/#1
var url = "http://example.com/#1";
var id = ; // id naumber (any date after hash 0, 00, 0000...)
if (window.location.hash == "#" + id + "") {
alert("your id is : " + id + "");
}
答案 0 :(得分:3)
怎么样:
/#(\d+)/
在第1组中捕获了ID
var url = "http://example.com/#1";
var id = /#(\d+)/;
if (url.match(id)) {
alert(url.match(id)[1]);
}