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

106
Visualizações
Remove last dots from characters in jquery

How to remove only the last dots in characters in jquery?

Example:

1..

1.2.

Expected result:

1

1.2

My code:

var maskedNumber = $(this).find('input.CategoryData');

var maskedNumberValue = $(maskedNumber).val().replace(/[^0-9.]/g, '').replace('.', 'x').replace('x', '.').replace(/[^\d.-]/g, '');

console.log(maskedNumberValue.slice(0, -1))

How do I solve this problem? Thanks

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

0

You can use regex replace for that:

function removeLastDot(value) {
  return value.replace(/\.*$/, '')
}

console.log(removeLastDot('1..'))
console.log(removeLastDot('1.2.'))

In the example I use \.*$ regex:

  • $ - means that I want replace at the end of string
  • \.* - means that I want to match any number for . symbol (it is escaped cause . is special symbol in regex)
about 4 years ago · Juan Pablo Isaza Relatório

0

You can traverse the string with forEach and store the last index of any number in a variable. Then slice up to that variable.

let lastDigitIndex = 0;
for (let i = 0; i < str.length; i++) {
   let c = str[i];
   if (c >= '0' && c <= '9') lastDigitIndex = i;
};
console.log(str.slice(0, lastDigitIndex-1));

This will be an optimal solution.

about 4 years ago · Juan Pablo Isaza Relatório

0

Add replace(/\.*$/g, '') to match one or more dots at the end of the string.

So your code would be like this:

var maskedNumberValue = $(maskedNumber).val().replace(/[^0-9.]/g, '').replace('.', 'x').replace('x', '.').replace(/[^\d.-]/g, '').replace(/\.*$/g, '');
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