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

239
Visualizações
de una cadena que contiene un intervalo de fechas

cuando naciste en 1995 también puedes hacer un amigo en 2002 pero tampoco exactamente en 1788. El resultado debería ser [1995, 1788]

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

0

Verifique este fragmento, utilicé una expresión regular para extraer todos los números del 0 al 9999 del texto, luego filtré todas las coincidencias para obtener solo los números dentro del rango dado.

 // This function will check if a value is inside a given range const isInRange = (lowerBound,upperBound,value) => { return (lowerBound<=value && value<=upperBound); }; // This is the main function that takes the input text as a parameter and retuns an array with all the years that was mentioned in the text and also are in the accepted range const getArrayOfYears = (text) => { const yearRegex = /[0-9]{1,4}/g; const matches = text.match(yearRegex); // This is a list of all the numbers in the list let res = []; matches.forEach( (item) =>{ const year = parseInt(item) if(isInRange(1900,2099,year)) { res.push(year); } }); return res; } const testText = "Usually people who were born in 1995 can find they first job not later than in 2020 but also not earlier than in 2012. Number 11999 is not included in the result because it's too big. It is out of range between 1900 and 2099."; // and here is the final result console.log(getArrayOfYears(testText))

about 4 years ago · Juan Pablo Isaza Relatório

0

No estoy seguro de que esta solución sea mejor que la de @Ali, pero es un enfoque diferente, donde todo se hace a través de RegExp. Lo admito, RegExp es un poco complicado para cubrir casos extremos...

 const getDates = (text) => { const datePattern = new RegExp(/(?:^|[^\d])((?:19|20)\d{2})(?:$|[^\d])/, 'g'); const results = []; let match; while ((match = datePattern.exec(text)) !== null) { results.push(parseInt(match[1])); } return results; };
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