I think what you want is the $highlight() identier
/help $highlight

An example
Code:
on *:text:*:*:{
  if $highlight($nick $1-)  {
    if !$window(@highlights) { window -n @highlights }
    linesep @highlights
    echo @highlights $timestamp $iif(#,$+($nick,:,#),$+($nick,:private)) $1-
    echo @highlights Matched: $highlight($nick $1-).text
    linesep @highlights
  }
}