Follow below:
var horariosFormatoInicial = '11:11 as 22:22 / 33:33 as 44:44 / 55:55 as 66:66';
var horariosSeparados = horariosFormatoInicial.replace(/ as /g, ' / ').split(' / ');
var saida1 = horariosSeparados.join(',');
var saida2 = '';
for (var i = 0; i < horariosSeparados.length; i++) {
saida2 += 'var' + (i + 1) + ' = "' + horariosSeparados[i] + '"; ';
}
Our variable horariosFormatoInicial
contains its original string, unformatted.
Then we create the variable horariosSeparados
which will be an array storing each time to use in the creation of saida1
and saida2
. In it we replace all the text strings as
with /
, so we will have all the times divided in the same format.
'11: 11 / 22:22 / 33:33 / 44:44 / 55:55 / 66:66 ';
Now we can use the function split()
, passing the character /
, to transform into our array.
[11:11, 22:22, 33:33, 44:44, 55:55, 66:66]
Now we can take our array and turn it into Output 1, using the function join()
, which takes each element of the array and separates it by the character entered, which in this case is the comma.
For output 2, we use for
to traverse our array and format the new string in the desired format.