Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

169
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!