Goes 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, unformatted string.
Then we create the variable horariosSeparados
which will be an array storing each time for use in creating the saida1
and saida2
. In it we replace all text snippets as
for /
, so we will have all the schedules 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 in 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()
, that takes each element of the array and separates it by the given character, which in this case is the comma.
For output 2, we use the for
to traverse our array and format the new string in the desired format.
Marconi, I’m gonna bother you a little :| o
+
is a quantifier that is equivalent to{1,}
, i.e., "1 or more". Of course without the modifier?
the+
He’s greedy, but I think if I don’t explain that the most is a quantifier, it can get confusing for anyone who reads it. Anyway the answer is good and already has my +1– fernandosavio
And it would be good to warn you that the regex would marry
123456:789
also, if he wants to limit to only 2 would have to use\d{2}:\d{2}
or\d\d:\d\d
.– fernandosavio
As it comes to schedules, I think I’d better use
{2}
instead of+
, since the number of digits looks like (by the description of the question) which will always be 2. I just found it strange that "33:33" and "44:44", etc. timetables, for me the maximum value of a time is 23:59. But anyway...– hkotsubo
Updated response, thanks guys :)
– Marconi
hkotsubo, these question values are only demonstrative of format, are not real values.
– Samuel Machado
Thank you, Marconi, for your reply and your message for your important comment
– Samuel Machado
Samuel, in this case, I suggest you always put values closer to the real ones, including invalid cases, because depending on the case you can have a more precise regex.
– hkotsubo