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

221
Visualizações
Split variable into several to get infos

I would like to separate this in Javascript and create variables infos1, infos2 ....

So I need a function to separate all the info.

<option value="[infos1][infos2][infos3][infos4]"></option>

var InfosInitial = $('#insert').find("option:selected").val();

var infos1 = ?;

var infos2 = ?;

Thanks you in advance !

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

0

You can use Regex to do this. We will use the String.match() function in this example. It should look something like this:

const InfosInitial = '[infos1][infos2][infos3][infos4]';
const info = InfosInitial.match(/(?<=\[)(.*?)(?=\])/gm);
const infos1 = info[0];
const infos2 = info[1];
const infos3 = info[2];
const infos4 = info[3];
console.log(`${infos1}\n${infos2}\n${infos3}\n${infos4}`);
console.log('As an array:', info);

Also, I am just saying but having everything in 4 different variables is inefficient. So you should use the array instead.

about 4 years ago · Juan Pablo Isaza Relatório

0

this is really hacky but you can add new variables to the window element like this

window['myVar'] = 'Hello, world!'

now you can call and redefine myVar by simply doing this

console.log(myVar)
// 'Hello, world!'

myVar = 42069
console.log(myVar)
// 42069

knowing this you can create a function where it gets the attribute 'value'

and split it between the ] and [ with String.split() remove the brackets and loop through the remainders

about 4 years ago · Juan Pablo Isaza Relatório

0

your question little unclear to me but I guess this is what you want.

var InfosInitial = "[infos1][infos2][infos3][infos4]";
Array.from(InfosInitial.matchAll(/\[([^\]]*)\]/g)).forEach(value => {
    console.log("all = " + value[0] + ", value = " + value[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