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

213
Vistas
Increment number in string in JS

I have this regex, but it does not do what I expect:

name.replace(/sor\s*(\d+)\.\s*szék/, function () {
  return arguments[1] * 1 + 1;
});

My string is 1. sor 1. szék. And I would expect a return 1. sor 2. szék

but the return value is: 1. 2. Why?

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

0

You get the value 1. 2 as the pattern sor\s*(\d+)\.\s*szék does not match the leading 1. so that will be untouched.

The part arguments[1] * 1 is 1, and then you add 1 to it resulting in 2.


If you want to use replace, you can use 3 capture groups and use those in the replacement, passing the group parameters to the callback function of replace.

The captured digits are in group 2, and to prevent concatenating 2 strings you can add a + before group 2.

let name = "1. sor 1. szék";
name = name.replace(
  /(sor\s*)(\d+)(\.\s*szék)/,
  (m, g1, g2, g3) => `${g1}${+g2+1}${g3}`
);
console.log(name);

about 4 years ago · Juan Pablo Isaza Denunciar

0

You could simplify the regular experession and replace only the second occurence of digits with an incremented value.

const
    string = '1. sor 1. szék.',
    result = string.replace(/\d+/g, (c => v => c-- ? v : +v + 1)(1));

console.log(result);

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