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

126
Visualizações
Validation of a range of floating point numbers

I have been looking for a long time and trying to write a regex, but not successfully.

Currently I have such a regex

/^([0-1](\.\d+)?)$/

and I would like it to validate if the number is between 0 and 1 along with the numbers after the decimal point.

The problem with the regex at the top is that it also accepts the numbers 1.11, 1.21. 1.3 etc.

I tried to do something like this but it doesn't work

/^([0(\.\d+)-1]?)$/

This regex also has to be universal because I will substitute a variable for the number between 0 and 1, and there may be different values there, e.g. 0-2.99, 3-13.21, and so on.

about 4 years ago · Santiago Gelvez
1 Respostas
Responde à pergunta

0

Using Regex to find a given number within the range of two numbers is like using a fork to scoop up water. Regex is used to find patterns within strings. You are using real numbers not strings. If you are comparing 3 variables that are numbers then you can use this simple function:

const range = (num, min, max) => min <= num && num <= max;

console.log(range(1.11, 0, 1));
console.log(range(2.991, 0, 2.99)); 
console.log(range(13.0000001, 3, 13.21));
console.log(range(5.69, 0, 6));

about 4 years ago · Santiago Gelvez 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