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

174
Vistas
I am getting error while Regex replace javascript

var newInput = '{"id":"1","value":"Admin","prefix":"@"} asdas {"id":"24","value":"Ibiere Banigo","prefix":"@"}';

var gettingJson = newInput.match(/\{\"(.*?)\"\}/g);
var finalString = '';
$.each(gettingJson, function (index, value) {
   var data = JSON.parse(value);
   finalString = newInput.replace(/\{\"(.*?)\"\}/g, '@[' + data.id + ']');
});
console.log(finalString);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

This is my code I am trying to replace this the parenthesis with @[id] it is replacing it but for all like I want my output to be

@[1] someone new @[2]

but instead I am getting

@[2] someone new @[2]
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Problem

The problem with your approach is the replace method replaces all matching occurrences.

Solution

Use replace method callback

replace(regexp, replacerFunction)

var newInput = '{"id":"1","value":"Admin","prefix":"@"} asdas {"id":"24","value":"Ibiere Banigo","prefix":"@"}';

var finalString = newInput.replace(/\{\"(.*?)\"\}/g, match => {
  var data = JSON.parse(match);
  return '@[' + data.id + ']'
})

console.log(finalString);

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