Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

221
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda