function guteLink(a) {
return a.replace(/.*?\/\?ref=([^\/]+).*$/g,'$1').split(/\/|#/)[0];
}
var lnk = "site.net.com/?ref=R111";
alert(guteLink(lnk) )
var lnk = "site.net.com/?ref=R111#post11";
alert(guteLink(lnk) )
var lnk = "site.net.com/?ref=R111/file/#post11";
alert(guteLink(lnk) )