Excellent, Cobra almost had it with his, but it fell over on substrings begining or ending in numbers, as the color codes had these in and would cause a missmatch in locating the start and end of the substring, but it was a clever laterally thought of idea i felt.


I had a feeling it would be a regex using the substring with the regex for any possable burk codes mixed in, but it was well beyond my knowledge of what to put in there. great to see it was doable.


I did need the $REPLEX but it wasnt working quite right
When the replace was done the first regsub was inserting the burk code (%s) regex between each character so * replaced with \* ended up getting one in between the \ & * etc

I fixed that pretty easy (see below incase you can improve it) now its running perfectly, at a rate 10 times faster than my original code.

Code:
replext {
  var %SubString = $2
  !.echo -q $regsub(%SubString,/(?<=.)(?=.)/g,$chr(26),%SubString)
  var %SubString = $replace(%SubString,\,\\,^,\^,$,\$,.,\.,[,\[,$chr(40),\ $+ $chr(40),$chr(41),\ $+ $chr(41),?,\?,*,\*,+,\+,$chr(123),\{,$chr(125),\})
  var %burkcodes = (?:[]|(?:\d\d?(?:,\d\d?)?)?)*
  var %SubString = $replace(%SubString,$chr(26),%burkcodes)
  var %ResultString
  !.echo -q $regsub($1,$+(/,%SubString,/gi),$replace($3,\,\\,$,\$),%ResultString)
  return %ResultString
}   
 


Thanks for the help with this, I must really get into regex more as it would appear to be able to solve several complexe problems very quickly.

Also thanks to Cobra and Iori (who was helping in email) for there greatfully accepted input on this.