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

115
Visualizações
Javascript regex remove trailing zeroes after decimal

I know this has been asked many times and I did find a solution, value.replace(/^([\d,]+)$|^([\d,]+)\.0*$|^([\d,]+\.[0-9]*?)0*$/, "$1$2$3").

But... I'm no regex expert so I'm just curious as to why this doesn't work /[\.0]+$/... 10 should return 10 but I just can't figure out how to exclude whole numbers that end with 0s.

1.7500, 1.1010, 1.0000, 10

1.75, 1.101, 1, 1

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

0

You might shorten the pattern to match either a dot with only zeroes, or use a non greedy match for the digits with a capture group and match optional trailing zeroes.

If you want to match digits and comma's the first \d+ can also be [\d,]+ as you already have in your pattern.

If there should be at least a single digit after the dot, then the quantifier can be a plus .\d+?

^(\d+)(?:\.0+|(\.\d*?)0+)$

See a regex 101 demo.

[
  "1.7500",
  "1.1010",
  "1.0000",
  "10"
].forEach(s =>
  console.log(s.replace(/^(\d+)(?:\.0+|(\.\d*?)0+)$/, "$1$2"))
);

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