-1

Given such a string +as[23+"AS@D"-@] replace all instances of @ between the [ and ] with the number right after the [ (in this case: 23). Please answer using JavaScript (a suggestion is using .replace()). So using this algorithm, +as[23+"AS@D"-@] will be changed to +as[23+"AS23D"-23]

Here is my regex \[(\d+)[^\]]*(\@*)\] The second capturing group is not captured (Regexr tells me that nothing is captured within the second capturing group). I need help on making the second capturing group capture all instances of @

I have tried using Mozilla Developer for help, but it did not work. Any help will be appreciated. Answers have to have explanation.

If this question is not clear enough, please tell me in the comments how can this be improved.

user41805
  • 503
  • 1
  • 9
  • 20

1 Answers1

0

You can use replace with a callback function in Javascript:

var s = '+as[23+"AS@D"-@]';
var r = s.replace(/\[(\d+)[^\]]*\]/, function($0, $1) {
                   return $0.replace(/@/g, $1);});
//=> +as[23+"AS23D"-23]

$0 represents whole string between [ and ] and $1 is the first capture group i.e. number right after [.

anubhava
  • 664,788
  • 59
  • 469
  • 547