I have a long string as shown below:
var tStr = "googletag.defineSlot('Lorem1', [300, 100], 'div-ad-1430201576110-0');
googletag.defineSlot('Lorem2', [300, 100], 'div-ad-1442774220958-0');
googletag.defineSlot('Lorem3', [300, 100], 'div-ad-1448024965037-0');
googletag.defineSlot('Lorem4', [290, 60], 'div-ad-1429688274462-0');
googletag.defineSlot('Lorem5', [300, 200], 'div-ad-1448279284710-0');
googletag.defineSlot('Lorem6', [728, 90], 'div-ad-1389610933954-2');
googletag.defineSlot('Lorem7', [728, 90], 'div-ad-1389610933954-4');
googletag.defineSlot('Lorem8', [300, 250], 'div-ad-1389610933954-0');
googletag.defineSlot('Lorem9', [300, 250], 'div-ad-1448278717664-0');
googletag.defineSlot('Lorem10',[300, 250] 'div-ad-1425381569546-1');
googletag.defineOutOfPageSlot('Lorem11','div-ad-1424524422308-0-oop1');
googletag.defineOutOfPageSlot('Lorem12','div-ad-1424524422308-0-oop2');
googletag.defineOutOfPageSlot('Lorem13','div-ad-1389610933954-0-oop4');
googletag.defineOutOfPageSlot('Lorem14','div-ad-1389610933954-0-oop5');
googletag.defineOutOfPageSlot('Lorem15','div-ad-1389610933954-0-oop6');";
From this string i need to fetch all last element passed in googletag.defineSlot().
For example for above string i need to get an array which should have ['div-ad-1430201576110-0','div-ad-1442774220958-0','div-ad-1448024965037-0','div-ad-1429688274462-0','div-ad-1448279284710-0','div-ad-1389610933954-2','div-ad-1389610933954-4','div-ad-1448278717664-0','div-ad-1425381569546-1']
So i am trying to something like:
var myregexp = /googletag.defineSlot+/g;
var match = myregexp.exec(x);
console.log(match);
but i know regex pattern is wrong. the correct approach or pattern i am looking for.
Thanks in advance
Assuming you can rely on div or div-ad in the last '...', here is the code and regex you can use:
/googletag\.defineSlot.*?'(div[^']*)'/g
See the regex demo
It matches:
googletag\.defineSlot - literally matches googletag.defineSlot.*? - a lazy dot matching construct (here it is good to use since the strings are short) matching as few as possible any characters other than a newline' - a single apostrophe(div[^']*) - capture group 1 matching div followed by any character but a '' - a literal '.If you can have any value in the last argument inside single quotes, use
googletag\.defineSlot.*?'([^']*)'\);
See another demo
var re = /googletag\.defineSlot.*?'([^']*)'\);/g;
var str = 'googletag.defineSlot(\'Lorem1\', [300, 100], \'div-ad-1430201576110-0\');\ngoogletag.defineSlot(\'Lorem2\', [300, 100], \'div-ad-1442774220958-0\');\ngoogletag.defineSlot(\'Lorem3\', [300, 100], \'div-ad-1448024965037-0\');\ngoogletag.defineSlot(\'Lorem4\', [290, 60], \'div-ad-1429688274462-0\');\ngoogletag.defineSlot(\'Lorem5\', [300, 200], \'div-ad-1448279284710-0\');\ngoogletag.defineSlot(\'Lorem6\', [728, 90], \'div-ad-1389610933954-2\');\ngoogletag.defineSlot(\'Lorem7\', [728, 90], \'div-ad-1389610933954-4\');\ngoogletag.defineSlot(\'Lorem8\', [300, 250], \'div-ad-1389610933954-0\');\ngoogletag.defineSlot(\'Lorem9\', [300, 250], \'div-ad-1448278717664-0\');\ngoogletag.defineSlot(\'Lorem10\',[300, 250] \'div-ad-1425381569546-1\');\ngoogletag.defineOutOfPageSlot(\'Lorem11\',\'div-ad-1424524422308-0-oop1\');\ngoogletag.defineOutOfPageSlot(\'Lorem12\',\'div-ad-1424524422308-0-oop2\');\ngoogletag.defineOutOfPageSlot(\'Lorem13\',\'div-ad-1389610933954-0-oop4\');\ngoogletag.defineOutOfPageSlot(\'Lorem14\',\'div-ad-1389610933954-0-oop5\');\ngoogletag.defineOutOfPageSlot(\'Lorem15\',\'div-ad-1389610933954-0-oop6\');';
var m;
var ar = [];
while ((m = re.exec(str)) !== null) {
ar.push(m[1]);
}
document.getElementById('result').innerHTML = JSON.stringify(ar, 0, 4);
<pre id="result"></pre>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With