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]
>Solution :
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);