Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6245431
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T12:30:18+00:00 2026-05-24T12:30:18+00:00

Everything in this code works properly, except the contents of the $1 variable aren’t

  • 0

Everything in this code works properly, except the contents of the $1 variable aren’t being properly displayed. According to my tests, all the matching is being done properly, I am just having trouble figuring out how to actually output the contents of $1.

  codeTags = {
              /\[b\](.+?)\[\/b\]/m => "<strong>#{$1}</strong>",
              /\[i\](.+?)\[\/i\]/m => "<em>#{$1}</em>"
             }

             regexp = Regexp.new(/(#{Regexp.union(codeTags.keys)})/)
             message = (message).gsub(/#{regexp}/) do |match|
               codeTags[codeTags.keys.select {|k| match =~ Regexp.new(k)}[0]]
             end

  return message.html_safe

Thank you!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T12:30:18+00:00Added an answer on May 24, 2026 at 12:30 pm

    As soon as you do this:

    codeTags = {
        /\[b\](.+?)\[\/b\]/m => "<strong>#{$1}</strong>",
        /\[i\](.+?)\[\/i\]/m => "<em>#{$1}</em>"
    }
    

    The #{$1} bits in the values are interpolated using whatever happens to be in $1 at the time. The values will most likely be "<strong></strong>" and "<em></em>" and those aren’t very useful.

    And regexp is already a regular expression object so gsub(/#{regexp}/) should be just gsub(regexp). Similar things apply to the keys of codeTags, they’re already regular expression objects so you don’t need to Regexp.new(k).

    I’d change the whole structure, you’re overcomplicating things. Just something simple like this would be fine for only two replacements:

    message = message.gsub(/\[b\](.*?)\[\/b\]/) { '<strong>' + $1 + '</strong>' }
    message = message.gsub(/\[i\](.*?)\[\/i\]/) { '<em>'     + $1 + '</em>'     }
    

    If you try to do it all at once you’ll have problems with nesting in something like this:

    message = 'Where [b]is[/b] pancakes [b]house [i]and[/i] more[/b] stuff?'
    

    You’d end up having to use a recursive gsub and possibly some lambdas if you wanted to properly handle things like that with a single expression.

    There are better things to spend your time on than trying to be clever on something like this.

    Response to comments: If you have more bb-tags and some smilies to worry about and several messages per page then you should HTMLify each message when you create it. You could store only the HTML version or both HTML and BB-Code versions if you want the BB-Code stuff around for some reason. This way you’d only pay for the HTMLification once per message and producing your big lists would be nearly free.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I implemented this Jquery show/hide code and everything works fine except that the box
I've tried everything from reading the Netbeans help to browsing Google. This code works
This isn't cross-platform code... everything is being performed on the same platform (i.e. endianess
The questions says everything, take this example code: <ul id=css-id> <li> <something:CustomControl ID=SomeThingElse runat=server
When this one runs everything goes fine: (r^newobject$, views.myobjects.newobject), All the CSS + JS
Hi Everytime I go directly to the app everything works properly. However when I
I have a situation where T4MVC is generating everything properly (meaning intellisense shows all
I've been trying to get this code to work for hours! All I need
I am trying to build a table using jQuery Datatables. Everything works on all
I am using PHP 5.3.6 I have the following code below. Everything works fine

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.