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

233
Vistas
JS Extract data from string

So I have the following string:

_string = 'From 00:00 To 01:23';

And I need to get the times in a format that I can work with, in order to save the data.

So I was thinking if I can get something like _string['from'] and _string['to'] and this way I can get the values and save them in the database.

So far I have tried .replace but I guess I don't have enough experience in JS in order to achieve what I'm looking for.

So this is what I have tried, I can get the To value, but didn't had any luck with from:

_string = 'From 00: 00 To 01:23';

res = _string.replace('From ','');
res2 = res.replace('To ','');

_one = _string.split('From ');
_two = _string.split('To');
console.log(_two[1]); //OUTPUS " 01:23"

Expected values: 00:00 for the From value and 01:23 for the TO value.

over 4 years ago · Santiago Trujillo
2 Respuestas
Responde la pregunta

0

You can use .replace() with RegExp /\s+/g to replace all space characters with an empty string, .match() with RegExp /\d+:\d+/g to match one or more digits followed by a colon followed by one or more digits

 var _string = 'From 00: 00 To 01:23'; var [from, to] = _string.replace(/\s+/g, "").match(/\d+:\d+/g); console.log(from, to);

over 4 years ago · Santiago Trujillo Denunciar

0

Try splitting on the empty space character.

var _string = "From 00:00 To 01:23";
var res = _string.split(' ');

// res == ['From', '00:00', 'To', '01:23'];

var outputFrom = res[1]; // 00:00
var outputTo = res[3]; // 01:23

console.log(outputFrom, outputTo);

over 4 years ago · Santiago Trujillo 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