This was for "@\S+" and "#\S+" while typing, but could be anything.
Was the tightest bit of code I could come up with and thought I'd share.
(see a lot of regex questions regarding this topic and there isn't much out there that really explains it well. The actual expression would depend on your need, likewise the criteria for a match and replace. Hopefully it helps someone else.)
3 items: array, textinput (set to invisible), and a textarea.