开发者

how to use javascript Regex to extract id: http://site/page?object_id=2

the url: http://site/page?object_id=2 I want the开发者_Python百科 number (id).


Try the following:

/\?(?:.*?&)?object_id=(\d+)/.exec(url)[1]

Unlike other answers, this will correctly handle http://site/page?other_object_id=3&object_id=2


If you have the URL in a string:

var str = "http://site/page?other_object_id=10&object_id=2";
var match = str.match(/[?&]object_id=(\d+)/);

if (match) {
  alert(match[1]); // 2
}

If it's the URL of the current page:

var match = location.search.match(/[?&]object_id=(\d+)/);

if (match) {
  alert(match[1]);
}


Try this:

var url = 'http://site/page?object_id=2';
var object_id = url.match(/object_id=(\d+)/)[1];


foo.match(/(\d+)/);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜