Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

112
Views
Grab characters before a specific word in string/cookie

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,

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

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

about 4 years ago · Juan Pablo Isaza Report

0

Try this.

var v = 'GAX1.3.yK3HnOfSRZCVUMNkK3tVCA';
var result = v.replace('GA','').replace('1.3.','');
console.log(result);
about 4 years ago · Juan Pablo Isaza Report

0

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

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!