Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

107
Visualizações
Extract only cookie's value from a string

I have a cookie stored as a string in a variable. This cookie is received from the API server and will vary each time. But for ease lets say the string is:

var csrfCookie = "XSRF-TOKEN=eyJpdiI6ImtBWXVqS0RmRzN5Z241UUVibXc4M1E9PSIsInZhbHVlIjoiMDJZYWNtUk5MOUxPZzA4d1JVMU5Ldks0R2VoWUhjMGNDREpXT1FrQjVqeHhRQ0FRQWtKUW1sTFY3MlRCVlhKS3kwczlOM2FKSWNXL3pNS0RCa3lvc3cxb0p5TGlSeXdDcjRMOFh5bytHakJmcU45S0JuT2pxOXdxSlY1WXdpQnQiLCJtYWMiOiI0OTlkNjgyNWNlMTJmZGJiMWM0ZWNmYzJmNTE3NGM5OTc5MjA3ZWQ2ODg2MDIyYmM4N2FiNTljMDQzYzQxYzBkIn0%3D; expires=Sun, 26-Sep-2021 10:15:25 GMT; Max-Age=7200; path=/; domain=.somedomain.site"

The string contains all data that is unnecessary for me like cookie name XSRF-TOKEN= at the beginning and ; expires=Sun, 26-Sep-2021 10:15:25 GMT; Max-Age=7200; path=/; domain=.somedomain.site at the end.

I want to remove this data from this and get only the remaining string (i.e cookie value) as result and also url decode the remaining resultant value.

How do I do it?

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

You should use regex expression to extract the value: const regex = /XSRF-TOKEN=(.*?);/;

var csrfCookie = "XSRF-TOKEN=eyJpdiI6ImtBWXVqS0RmRzN5Z241UUVibXc4M1E9PSIsInZhbHVlIjoiMDJZYWNtUk5MOUxPZzA4d1JVMU5Ldks0R2VoWUhjMGNDREpXT1FrQjVqeHhRQ0FRQWtKUW1sTFY3MlRCVlhKS3kwczlOM2FKSWNXL3pNS0RCa3lvc3cxb0p5TGlSeXdDcjRMOFh5bytHakJmcU45S0JuT2pxOXdxSlY1WXdpQnQiLCJtYWMiOiI0OTlkNjgyNWNlMTJmZGJiMWM0ZWNmYzJmNTE3NGM5OTc5MjA3ZWQ2ODg2MDIyYmM4N2FiNTljMDQzYzQxYzBkIn0%3D; expires=Sun, 26-Sep-2021 10:15:25 GMT; Max-Age=7200; path=/; domain=.somedomain.site";

const regex = /XSRF-TOKEN=(.*?);/;

var matches = regex.exec(csrfCookie);
console.log(matches[1]);

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda