Grab characters before a specific word in string/cookie

Viewed 97

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,

4 Answers

Try this.

var v = 'GAX1.3.yK3HnOfSRZCVUMNkK3tVCA';
var result = v.replace('GA','').replace('1.3.','');
console.log(result);

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\.([^.]+)

regex diagram
regexper.com

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);
});

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);

  1. Split each string at every dot inside the read array into a new array called say, cookieParts.

     read.forEach(e => {
       let cookiePart = e.split('.');
     }
    
  2. Instead of using a hard-coded number [3] for getting the variantID, just get the last index of each split string instead.

     read.forEach(e => {
       let cookiePart = e.split('.');
       let variantID = cookiePart[cookiePart.length - 1];
     }
    
  3. Use the includes() method to check if the split string has GAX1.3 or not and if it does, log the third element to the console else log the first element to the console.

     read.forEach(e => {
       let cookiePart = e.split('.');
       let variantID = cookiePart[cookiePart.length - 1];
    
       console.log("variantID", variantID);
    
       if(e.includes('GAX1.3')) {
         console.log('id', cookiePart[2]);
       } else {
         console.log('id', cookiePart[0]);
       }
     }
    

Check and run the following Code Snippet for a practical example of the above approach:

const cookie = 'GAX1.3.yK3HnOfSRZCVUMNkK3tVCA.18961.ss12!QV1ffpISQcCGmM4IOTuUlQ.18961.0!F9l4roGRR5K03cSaZMWQiA.18961.1';

let read = cookie.split("!");

read.forEach(e => {
  let cookiePart = e.split('.');
  let variantID = cookiePart[cookiePart.length - 1];
  let id = e.includes('GAX1.3') ? cookiePart[2] : cookiePart[0];

  console.log("variantID", variantID);
  console.log("id", id);
});

P.S. In the above snippet, I used a ternary operator instead of an if/else statement for retrieving the id based on whether GAX1.3 is present or not. You can replace the ternary operator with an if/else statement in case you prefer if/else over ternary.

Related