Reputation: 14145
I'm intercepting user clicks on pagination links and I need to extract only page number from that link which is always set inside page variable.
so inside my js I have ....
var a = $a.attr("href");
.. which ouputs this
/Home/GetTabData?page=2&activeTab=House
so how can I extract only this number from after page=
If it's matter keep in mind that this number can be two or three digit long and activeTab is dynamic value.
Thanks
Upvotes: 0
Views: 54
Reputation: 66663
Using some regex, you can extract it like:
var num = +(a.match(/page=(\d+)&/)[1]);
In the above code:
/page=(\d+)&/
is a regex that matches the number (one or more numeric characters) between "page="
and "&"
. Note that we have grouped the (\d+)
which is the number we are after
The +
prefix is for converting the extracted string into a number. This is similar to using parseInt()
Upvotes: 2
Reputation: 2857
after you have the link, in this case a, you simply search the string for 'page=' using indexOf() then make a substring from there to the next & character.
var start, end, value;
start = a.indexOf("page=");
end = a.indexOf("&", start);
value = a.substring(start+5, end);
more info on IndexOf() http://www.w3schools.com/jsref/jsref_indexof.asp
Upvotes: 0