I have a cookie (_gaexp), which I want to extract values from:
GAX1.3.yK3HnOfSRZCVUMNkK3tVCA.18961.ss12!QV1ffpISQcCGmM4IOTuUlQ.18961.0!F9l4roGRR5K03cSaZMWQiA.18961.1
I specifically want to extract the characters, before 18961 only, which I can do.
But I don't want GAX1.3 included in my result.
For example, I have the following code:
let read = readCookie("_gaexp").split("!");
read.forEach(function(key, index) {
splitBeginningOfCookie = read[0].split('.')
optimizeID = splitBeginningOfCookie[3];
let variantID = key.split(optimizeID+".")[1];
let id = key.split("."+optimizeID)[0];
console.log("variantID", variantID);
console.log("id", id);
});
Which console.logs:
variantID ss12
id GAX1.3.yK3HnOfSRZCVUMNkK3tVCA
variantID 0
id QV1ffpISQcCGmM4IOTuUlQ
8 variantID 1
id F9l4roGRR5K03cSaZMWQiA
I want to exclude GA1.3. from the first loop though. What is the best way, to grab the long IDs only, from this string?
The last 2 loops are fine. It's just the first one, which returns everything before 18961.
Thanks,
Instead of using split() you could use a regex to grab the two sections around .18961.. In this scenario you could use the regex:
([^.]+)\.18961\.([^.]+)
const cookie = "GAX1.3.yK3HnOfSRZCVUMNkK3tVCA.18961.ss12!QV1ffpISQcCGmM4IOTuUlQ.18961.0!F9l4roGRR5K03cSaZMWQiA.18961.1";
const regex = /([^.]+)\.18961\.([^.]+)/;
const read = cookie.split("!");
read.forEach((part, index) => {
const [_, id, variantID] = part.match(regex);
console.log("variantID", variantID);
console.log("id", id);
});
Try this.
var v = 'GAX1.3.yK3HnOfSRZCVUMNkK3tVCA';
var result = v.replace('GA','').replace('1.3.','');
console.log(result);
Maybe with a help of RegExp:
const data = "GAX1.3.yK3HnOfSRZCVUMNkK3tVCA.18961.ss12!QV1ffpISQcCGmM4IOTuUlQ.18961.0!F9l4roGRR5K03cSaZMWQiA.18961.1";
const reg = /([^.!]+)\.18961\.([^!]+)/g;
const result = {};
let d;
while((d = reg.exec(data)))
result[d[1]] = d[2];
console.log(result);
Or using matchAll()
const data = "GAX1.3.yK3HnOfSRZCVUMNkK3tVCA.18961.ss12!QV1ffpISQcCGmM4IOTuUlQ.18961.0!F9l4roGRR5K03cSaZMWQiA.18961.1";
const reg = /([^.!]+)\.18961\.([^!]+)/g;
const result = [...data.matchAll(reg)].reduce((r,a)=>{return r[a[1]]=a[2],r},{});
console.log(result);