gsub question, not a regex question...including part of the original in the sub...

Hi all,

I've got a simple bulletin board type of code in part of my
application, and I want to implement phpbb-like tags for users to add
to posts. I strip HTML out of the posts, of course. I can handle
things like [b] => <b> with a simple gsub, but there's one that is
throwing me for a loop. I want users to be able to quote other users,
and have the quoted text show up with an "originally posted by
username" and some CSS code. I can gsub the [/QUOTE] into a </div>
but how do I change [QUOTE=username] into:

<div class="quote"><p><em>Originally posted by username</em></p>

Yes, I can do most of it with a gsub, but how do I pull "username" out
of the original text and put it where it belongs in the substituted
text?

Or is there a better way to do this all together?

Thanks!

Shortened to not wrap:

"[q=u]".gsub(/\[q=(\w+)\]/, '<div class="q">blah \1</div>')

=> "<div class=\"q\">blah u</div>"

( ) are used for grouping in regexes and \1, \2, etc. are used to reference the groups in the replacement pattern.

So (\w+) creates a group of all the "word" characters after the q= and before the ] and puts that into \1.