You could re-run this regex replace until no more replacements are made.
Regex: (\[(?:<a(?=\s|>)(?:[^>=|&)]|='(?:[^']|\\')*'|="(?:[^"]|\\")*"|=[^'"][^\s>]*)*>.*?<\/a>,)*)(\d+)([,\]])
Replace with: $1<a href="ref$2">$2</a>$3
The portion which captures group 1, will match event the most sophisticated complex anchor tags.
Examples
Sample Text
[22][44,45,77,91]
After Replacement
First time:
[<a href="ref22">22</a>][<a href="ref44">44</a>,45,77,91]
Second time:
[<a href="ref22">22</a>][<a href="ref44">44</a>,<a href="ref45">45</a>,77,91]
Third time:
[<a href="ref22">22</a>][<a href="ref44">44</a>,<a href="ref45">45</a>,<a href="ref77">77</a>,91]
Fourth time:
[<a href="ref22">22</a>][<a href="ref44">44</a>,<a href="ref45">45</a>,<a href="ref77">77</a>,<a href="ref91">91</a>]