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

122
Visualizações
Regex pattern to not to include zeros

I need to match a pattern for validation . I want to match a decimal number where numeric part can have upto 14 digits including + or - without 0 and after decimal has upto 4 digits. Valid patterns are :

+1.23
9857.6543
-745290.0

Invalid patterns are:

 0
 0.00
 1.23456

I have tried ^[0-9]{0,14}\.[0-9]{0,4}$. I am not getting how to match for +,- and 0 condition

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

0

Short answer: ^[+-]?[1-9][0-9]{0,13}\.[0-9]{1,4}$


^ - start of string

[+-]? optionally, one between + and -

[1-9][0-9]{0,13} - a 14 digit number that doesn't start with 0

\. - decimal separator, has to be escaped or it will mean "any one character"

[0-9]{1,4} - up to 4 decimal digits

$ - end of string

about 4 years ago · Juan Pablo Isaza Relatório

0

The pattern:

^[+-]?[^\D0]\d{0,13}\.\d{1,4}(?!\d)

matches the first 3 but not the second 3. [^\D0] is, if I'm not mistaken, strictly the same as [123456789], but slightly more compact.

about 4 years ago · Juan Pablo Isaza Relatório

0

This might work ^(\+|-)?(([1-9]|0(?=0*[1-9]))[0-9]{0,13}(\.[0-9]{1,4})?|0{1,14}\.(?=0*[1-9])[0-9]{1,4})$

  • ^(\+|-)? - starts with +/-
  • (
  • ([1-9]|0(?=0*[1-9]))[0-9]{0,13}(\.[0-9]{1,4})? - absolute value >= 1
  • | - or
  • 0{1,14}\.(?=0*[1-9])[0-9]{1,4} - 0.**** with at least 1 non-zero digit
  • )
  • $ - end

const testcases = [
    '+1.23', 
    '9857.6543', 
    '-745290.0', 
    '1.0', 
    '1.00', 
    '12', 
    '0.01', 
    '+001.01', 
    '0', 
    '0.00', 
    '1.23456', 
    '0.0', 
    '12.'];
 
const regex = /^(\+|-)?(([1-9]|0(?=0*[1-9]))[0-9]{0,13}(\.[0-9]{1,4})?|0{1,14}\.(?=0*[1-9])[0-9]{1,4})$/;

testcases.forEach(n => console.log(`${n}\t - ${regex.test(n)}`));

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