I don't see what @target@[1] is supposed to be replaced by in that example.

It seems you're trying to replace values multiple times in a single line, I think this may best be done outside of the main replacement regex. Identify all instances of commands and send those each to the replacement alias. This new regex makes use of the recursive pattern (?R) to capture properly matched brackets, which it performs well on. Improperly matched brackets it falls apart, so that could use some work.

Aside from the @target@ command which differs in its syntax I have added to the script below. It now uses $isalias as you have done and I have made a direct semi-recursive call to avoid working around injection.

Code:
alias test {
  tokenize 32 here is $!pi some @upper@[@bold@[@pi@]] right here noop @underline@[@pi@]
  var %regex = /(@.+?@\[(?:[^\[\]]|(?R))*\])/g

  echo -ag $regsubex(recurse,$1-,%regex,$twitch.sub($regml(recurse,\n)))
}

alias twitch.sub {
  var %regex = /^@(\S+?)@(?:\[(.*?)\])?$/iS

  if (!$regex($1-,%regex)) return

  var %command = $regml(1)
  var %args = $regml(2)

  if (%args != $null) && ($regex(%args ,%regex)) {
    %args = $twitch.sub.recurse(%args)
  }

  %command = twitch.val. $+ %command

  if ($isalias(%command)) {
    %command %args
    return $result
  }
  else return $1-
}

alias twitch.sub.recurse return $twitch.sub($1-)

alias twitch.val.upper return $upper($1-)
alias twitch.val.underline return  $+ $1- $+ 
alias twitch.val.bold return  $+ $1- $+ 
alias twitch.val.pi return $pi

Last edited by Loki12583; 09/12/15 03:02 AM.