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 6811505
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T20:20:11+00:00 2026-05-26T20:20:11+00:00

Suppose I have a body of text (string) like this: str = ‘This is

  • 0

Suppose I have a body of text (string) like this:

str = ‘This is some text. {insertion_1} And this is also some text. {insertion_2}’

The contents of the two curly braces represent a model (Insertion) and its ID. The ultimate goal is to parse the string, and replace the curly braced “insertions” with an insertion partial. The insertion partial will simply be a line or two of HTML.

If the insertions were static I could do something like str.gsub!(/\{insertion_\d*\}/, 'some content'), but I need to parse the insertions one-by-one, to insert the appropriate data. Can anyone suggest a best practice for handling a situation like this?

EDIT: I should have mentioned, this is for a WYSIWYG. The end user selects from a list of “insertions” and once selected, it adds the appropriate {insertion_id} placeholder into the body of their post, which will be parsed out later to insert the right content.

  • 1 1 Answer
  • 1 View
  • 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-26T20:20:12+00:00Added an answer on May 26, 2026 at 8:20 pm

    You can use regexp captures and gsub with a block to accomplish what you want, like this:

    str = 'This is some text. {insertion_1} And this is also some text. {insertion_2}'
    
    replacements = {
      1 => 'HELLO',
      2 => 'WORLD',
    }
    
    str.gsub(/\{insertion_(\d*)\}/) {
      id = $1.to_i
      replacements[id]
    }
    # => "This is some text. HELLO And this is also some text. WORLD" 
    

    Just replace the block body with whatever you need to do. 🙂

    (Sidenote: \d+ is a better choice than \d* for matching numbers.)

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

Sidebar

Related Questions

Suppose I have a class that looks like this: class Derived : // some
Suppose you have something like this HTML FILE: <head></head> <body> <div id=my_personal_div> </div> </body>
Suppose you have a text-field in your webpage. The value inside text-field is This
Suppose I have a document like <style type=text/css> div { border:1px solid; padding:15px; }
Suppose I have following html page. <html> <head> <link type=text/css rel=Stylesheet href=css/some.css /> </head>
Suppose I have the following CSS rule in my page: body { font-family: Calibri,
Suppose i have JSF page <h:body> <h:form id=formID prependId=false> <h:outputText id=randomID value=#{randomNumber.random}/> <h:commandButton id=buttonID
Suppose I have a string which contains an HTML file. How do I get
Suppose I have a DB of users and I have two main functions: to
Let suppose i have two html pages p1 & p2 and it uses different

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.