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

220
Views
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 answers
Answer question

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 Report

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 Report

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 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!