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

58
Visualizações
Grab substring after and before two specific characters in javascript

i have a url search key like that: ?retailerKey=A and i want to grab the retailerKey substring. All examples that i saw are having as example how to take before the char with the indexOf example. How can i implement this to have this substring from the string ?retailerKey=A

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

0

You could split() the string on any ? or = and take the middle item ([1]) from the outcome array.

const data = "?retailerKey=A";
const result = data.split(/[\?=]/)[1];

console.log(result);

If you have multiple params, creating an object fromEntries() would be interesting.

const data = "?retailerKey=A?otherKey=B";

const keyVals = data.split(/[\?=]/).filter(x => x); // keys and values
const result = Object.fromEntries(keyVals.reduce((acc, val, i) => {
  // create entries
  const chunkI = Math.floor(i / 2);
  if (!acc[chunkI]) acc[chunkI] = [];
  acc[chunkI].push(val);
  return acc;
}, []));

console.log(result);

about 4 years ago · Juan Pablo Isaza Relatório

0

Using library could be a better choice but to do it from scratch : I suggest to use split with a regular expression.

// split for char equals to ? or & or =;
const url = '/toto?titi=1&tata=2';
const args = url.split(/[\?\&\=]/);
// shift the first element of the list since it the base url before "?"
args.shift();

// detect malformed url
if (args.length % 2) {
    console.error('malformed url', args);
}

const dictArgs = {};

for (let i = 0; i < args.length /2; i ++) {
     const key = args[2*i];
     const val = args[2*i+1];
     dictArgs[key] = val;
}

console.log(dictArgs);
    
about 4 years ago · Juan Pablo Isaza Relatório

0

use regex expression. Following will return the value between character ? and =

var result =  "?retailerKey=A".match(/\?(.*)\=/).pop();
console.log(result);

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