the url: http://site/page?object_id=2 I want the number (id).
+2
A:
Try this:
var url = 'http://site/page?object_id=2';
var object_id = url.match(/object_id=(\d+)/)[1];
Tatu Ulmanen
2010-02-24 18:52:28
I would just take the $ away, your \d+ should work fine. Also put the + inside the parentheses - `(\d+)`.
Renesis
2010-02-24 18:54:52
+2
A:
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]);
}
CMS
2010-02-24 18:54:36
SLaks
2010-02-24 19:18:20