-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.

4

1 回答 1

0

您可以replace在 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表示和之间的整个字符串[]并且$1是第一个捕获组,即紧随其后的数字[

于 2015-11-08T16:22:34.770 回答