Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

216
Vistas
Is there any way to add number separated in html using regex and jquery?

I want to add a specific number to all the numbers that are separated by commas in HTML using regex and jquery.

here is my trial code -

$('#u-g-wb-break-word').text(function(i, s) {
  return s.replace(/\d+/g, function(m) {
    return parseInt(m, 10) + 612;
  });
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="u-g-wb-break-word">
  123,451,45.00
</div>

This does work but not in the case of float numbers, In decimal numbers, the script is adding the value after decimal too. To change this I also used /m instead of 'g' but still, it's only working for one number and not all the numbers are separated by commas.

$('#u-g-wb-break-word').text(function(i, s) {
  return s.replace(/\d+/m, function(m) {
    return parseInt(m, 10) + 612;
  });
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="u-g-wb-break-word">5000.1000</div>

The above code is working but only if I put a single number in the HTML, How can I fix the code. Thanks in advance

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Use a regular expression that matches numbers with an optional fraction. Use capture groups to get the integer part and the fraction separately, then just add to the integer.

$('#u-g-wb-break-word').text(function(i, s) {
  return s.replace(/(\d+)(\.\d+)?/g, function(m, int, fraction) {
    return (parseInt(int) + 612) + (fraction || "");
  });
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="u-g-wb-break-word">
  123,451,45.00
</div>

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda