Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

220
Visualizações
Get all (H)H:MM timestamps from a string using RegEx

I am looping through strings that include two 24h timestamps without leading 0. A string looks like this

"FooBar Examplestring 4:34 - 12:30"

I understand that the regex to match a 24 hour timestamp with optional leading zero is

^([0-9]|0[0-9]|1[0-9]|2[0-3]):[0-5][0-9]$

What I do not understand is, and that is some basic RegEx knowledge that I am missing here, is how I would get the two timestamps as matches from the first string.

I am using javascript btw. Here's sample code: https://jsfiddle.net/7dbfeL21/1

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

Using match with the regex pattern \d{1,2}:\d{2} we can try:

var input = "FooBar Examplestring 4:34 - 12:30";
var matches = input.match(/\d{1,2}:\d{2}/g);
console.log(matches);

The pattern \d{1,2}:\d{2} is a simplification of what you were using. But given that is is unlikely that anything else in your input would match this, you might find this an easier approach.

about 4 years ago · Juan Pablo Isaza Relatório

0

You have to remove the anchors ^ and $ and use word boundaries instead. You can shorted the pattern, making the first zero optional.

\b(?:0?[0-9]|1[0-9]|2[0-3]):[0-5][0-9]\b

Regex demo

let regex = /\b(?:0?[0-9]|1[0-9]|2[0-3]):[0-5][0-9]\b/g;
let str = "FooBar Samplestring 2:30 - 14:34";
var matches = str.match(regex);
console.log(matches);

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda