Gsub question, not a regex question...including part of the

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] => 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
but how do I change [QUOTE=username] into:

Originally posted by username

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!

[email protected] wrote:

Originally posted by username

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?

Shortened to not wrap:

“[q=u]”.gsub(/[q=(\w+)]/, ‘

blah \1
’)

=> “<div class=“q”>blah u

( ) 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.


Michael W.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs