How do you capture the time in a String?

Asked

Viewed 76 times

1

I am making a javascript code where I will need to divide a sentence so that I look for the hours, I just want their value and so that I can separate them, to put the value separately.

Ex: 

String: 11:11 as 22:22 / 33:33 as 44:44 / 55:55 as 66:66

Saída: 11:11,22:22,,33:33,44:44,55:55,66:66

Saída 2: var1 = "11:11"; var2 = "22:22"; var3 = "33:33"; var4 = "44:44"; var5 = "55:55"; var6 = "66:66";

2 answers

7


This simple regex solves your problem: /\b\d{2}:\d{2}\b/g

let texto = '11:11 as 22:22 / 33:33 as 44:44 / 55:55 as 66:66'
const expressao = /\b\d{2}:\d{2}\b/g

console.log(texto.match(expressao))

  • \b to what examples as 123456:789 are not returned
  • \d shortcut to [0-9]
  • {2} quantifier that takes exactly two numbers.
  • g flag to find all possible results.

Detail: The result results in an array, ai just use it the way you want!

  • 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

  • 1

    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.

  • 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...

  • Updated response, thanks guys :)

  • hkotsubo, these question values are only demonstrative of format, are not real values.

  • 1

    Thank you, Marconi, for your reply and your message for your important comment

  • 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.

Show 2 more comments

1

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.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.