-2

Here is a example of my link:

var eg="http://abcd.efgh.com/ar/1/55242/1?lt=9&adtype=3&pubid=5574988682&toolid=10001&campid=5337040086&customid=&uq=HO+scale+scenery&sellerId=&ex_kw=&sortBy=1&catId=&minPrice=&maxPrice=&laction=_self&ltext=Scenery&n3y=1&v1e=1&u7v=1&a3h=1&def=a3h&ig=1&mpt=366074929"

I want pubid and campid in variables by using regex pattern.

var pubid=5574988682
var campid=5337040086
halfer
  • 19,824
  • 17
  • 99
  • 186
anshul gupta
  • 28
  • 11
  • javascript or jquery ??... – Pranay Rana Sep 21 '17 at 06:09
  • 2
    Please share the code & logic that you have tried and got stuck. Thanks! – palaѕн Sep 21 '17 at 06:09
  • It is worth noting that regex questions without any attempt whatsoever are posted frequently onto Stack Overflow, and you may get a poor reception when making such posts, given that people will think you're asking them to do your work. Please always show your working, especially with regex questions. Let us know what you are stuck on in particular. – halfer Sep 21 '17 at 20:10

4 Answers4

2

you can try like this

// Read a page's GET URL variables and return them as an associative array.
function getUrlVars()
{
    var vars = [], hash;
    var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
    for(var i = 0; i < hashes.length; i++)
    {
        hash = hashes[i].split('=');
        vars.push(hash[0]);
        vars[hash[0]] = hash[1];
    }
    return vars;
}

above code taken form : http://jquery-howto.blogspot.in/2009/09/get-url-parameters-values-with-jquery.html

Pranay Rana
  • 175,020
  • 35
  • 237
  • 263
1

You could do this:

var queryDict = {}
eg.search.substr(1).split("&").forEach(function(item) {queryDict[item.split("=")[0]] = item.split("=")[1]})

And then:

queryDict.pubid
=> 5574988682

queryDict.campid
=> 5337040086
Graham Slick
  • 6,692
  • 9
  • 51
  • 87
1

You should try

regex = /.*&pubid=([\w-]{10}).*&campid=([\w-]{10})&.*/; //Regex

url = 'http://abcd.efgh.com/ar/1/55242/1?lt=9&adtype=3&pubid=5574988682&toolid=10001&campid=5337040086&customid=&uq=HO+scale+scenery&sellerId=&ex_kw=&sortBy=1&catId=&minPrice=&maxPrice=&laction=_self&ltext=Scenery&n3y=1&v1e=1&u7v=1&a3h=1&def=a3h&ig=1&mpt=366074929'; //Your URL

pubid = url.match(regex)[1];  //Get your pubid from first parameter
campid = url.match(regex)[2];   //Get your campid from second parameter</code>

You will get pubid = 5574988682 and campid=5337040086

0

For camp id RegEx is

(campid=)(\d)+

For pubid RegEx is

(pubid=)(\d)+

Where pubid= and campid= are strings and

\d---->Matches any digit character (0-9). Equivalent to [0-9].

+----->Matches 1 or more of the preceding token.

Dev
  • 378
  • 2
  • 16