How to prevent partial match

How to prevent partial match

Hi

Bit of a noob question, but I'm using the following code to get a menu tab to highlight/switch class automatically based on the id of the page in the querystring. 
 
All is working well until the page ids hit double digits. Subsequently a menu with the link "p=10" and above will highlight when "p=1" in the location bar. How should I amend the code to prevent this partial match?
The menu item links always end after "p="; there are no further keys in the querystring to act as a delimiter.
 
Any pointers greatly appreciated! 
  1. $(
    function() {

    var query = location.search;
    var qstring = '?p=';
    var pid = query.substring(query.indexOf("p=") + 2, query.length);

    pid = pid.split(

    '&');
          if (pid[0]) {

                $(

    '#primarymenu li a[href*="p=' + pid[0] + '"]').toggleClass('myselected');

          }

    }