JavaScript URL哈希检测ID号

时间:2012-03-02 18:27:39

标签: javascript regex

我需要在此代码中编写正则表达式来检测网址上的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 + "");
}

1 个答案:

答案 0 :(得分:3)

怎么样:

/#(\d+)/

在第1组中捕获了ID

var url = "http://example.com/#1";
var id = /#(\d+)/;
if (url.match(id)) {
     alert(url.match(id)[1]);
}
相关问题