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 !
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.
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
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]);
})