The problem with this solution is that if the 3rd (or last) character is a number it will mess up, because the two terminating ^K's would actually start another colour. To see what I mean, try your regex with "ab3def7".

The simplest workaround would be this:
Code:
$regsub(%x,/^(.)(.)(.*)(.)(.)$/g,$+(\104\2,$str($chr(2),2),\304\4,$str($chr(2),2),\5),%x)
We insert ^B pair after the terminating ^K, preventing it from colouring anything we don't want. I made it with $str(...) so that it's more readable.

Another thing, this code only works with a single word; it can't be used to colour the letters in each words, in a string like "hello world!". To do so, and also get rid of some backreferences, the $regsub() should look like this:
Code:
$regsub(%x,/(?<=^\S| \S)(\S)(\S*)(\S)(?=\S$|\S )/g,04\1\204\3,%x)
I know he didn't ask for help about code that works with a sentence and not just a word, I just couldn't help writing this version too :tongue:

However, this way a ^B pair is inserted even when there is no need to (because the 3rd or last char is not a number, so ^K would really be considered a terminating code). It is possible to check exactly this though, and not add a ^B when it's not necessary. Here's what I came up with:
Code:
alias color2chars {
  var %a = /(?<=^\S| \S)(\S)(\d?)(\S*)(\S)(?:(\d)()|()(\S))(?=$| )/g
  if $regsub($1,%a,$+(04\1,$cr,\2,$lf,\304\4,$cr,\5,$lf,\6),%a) {
    return $replace($remove(%a,$crlf,$lf),$cr,)
  }
  return $1
}
The $cr's and $lf's are used as temporary boundaries/signposts. I chose the two safest chars IRC-wise (they cannot be part of an IRC message so it's very unlikely that the user would even want to feed that $regsub() with a word that contains those chars). The regex pattern may look a bit odd but I made it so to avoid a 2nd $regsub() call and only use $replace()/$remove().

There's always a trade-off: the first way is faster, as it only calls one $regsub(), but produces an "imperfect" string, ie one that may contain redundant ^B pairs. The second way is slower since it calls a more complex $regsub() and a $replace()/$remove(), but produces a perfect result. So I guess it depends on where it's going to be used: if it's for an on INPUT or something, where the alias would be called once, I'd go for the 2nd way. If the alias is going to be called a lot of times (fex inside a loop), I'd choose the 1st.


/.timerQ 1 0 echo /.timerQ 1 0 $timer(Q).com