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

232
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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