in reply to Re^2: interpolating code in regex brace quantifier
in thread interpolating code in regex brace quantifier

Note that you can also achieve the desired result through recursion
(a(?1)?x)