iRules uses regsub to do URL conversions

I’d like to automatically convert URLs, i.e

I’ve tried following code snippet in TCL

set loc "https://sc-uat.ct.example.com/sc/"
 
set envs(dev) "test"
set envs(uat) "beta"
set envs(qa) "demo"
 
puts $envs(uat)
regsub -nocase {://.+-(.+).ct.example.com} $loc {://inv[$envs(\1)].example.com} hostname
 
puts "new location = $hostname"

But the result is: new location = https://inv[$envs(uat)].example.com/sc/ It seems that [$envs(uat)] is NOT eval-ed further.

Any hints will be appreciated. Thanks in advance

I’ve got a very nice answer from https://stackoverflow.com/questions/58338105/tcl-regsub-uses-regex-match-as-index-in-associate-array