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

  • Home
  • SEARCH
  • 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 9036271
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T08:57:00+00:00 2026-06-16T08:57:00+00:00

I’m looking for a way to properly replace nested custom tags with their HTML

  • 0

I’m looking for a way to properly replace nested custom tags with their HTML equivalents. For example, suppose we have the following text:

This is {b:bold text}

Which should become:

This is <b>bold text</b>

I’m aware that I can – and probably should – use something like a span with a “bold” class instead of the old “b” tags, but there’s a reason I’m sticking with “b” for the purpose of this example. I may have nested tags:

This is {b:bold text and {i:italic}}

which should become:

This is <b>bold text and <i>italic</i></b>

However, I have these regexes (which I use with the replace() function):

/\{b:([\s\S]*?)\}/gm
/\{i:([\s\S]*?)\}/gm

And then the result will become:

This is <b>bold text and <i>italic</b></i>

The tags are not properly nested. Again, using spans can solve this in this case, but that won’t work with things like “ul”, “li”, “h1”, …
Greedy regexes will also cause problems if the text looks like this:

This is {b:bold text} and {i:italic}

So my current solution is to replace everything with span first, using a data-tag attribute to indicate what it should actually be, and then use jQuery to find all span nodes and replace them with the proper tags:

{h:This is a header}
becomes
<span data-tag='h1'>This is a header</span>
becomes
<h1>This is a header</h1>

It works well, but i’m wondering if there is an easier way to do this. The intermediate method feels a bit like a duct tape solution and I would like to “beautify” it.

Any suggestions?

  • 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-06-16T08:57:01+00:00Added an answer on June 16, 2026 at 8:57 am

    Ditch the Regex and use one of the many templating tools out there, such as Mustache, or t.js.

    EDIT: Fixed!

    …but if you are avoiding such simplicities in life, here’s how to do it with a regular expression:

    var rex = /\{(b|i):([^{}]*)\}/igm,
        parse = function(txt) {
            var cnt = 0,
                more = true;
            while (more) {
                txt = txt.replace(rex, function(match, $1, $2) {
                    return "<" + $1 + ">" + $2 + "</" + $1 + ">";
                });
                more = rex.test(txt);
                cnt++;
            }
            return txt;
        };
    

    So you call parse and pass it your string. It recursively parses your string until no other mark-up is found. This new one works in reverse, parsing out the items that don’t have items within, then moving outward.

    To implement the H1 example that you added, you could either instead us {h1:This is the header} and add it to the (b|i) clause, or use a switch statement in the replace function instead of just returning $1.

    Here’s the JSFiddle.

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

Sidebar

Related Questions

I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
I have a jquery bug and I've been looking for hours now, I can't
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have this code to decode numeric html entities to the UTF8 equivalent character.
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
i got an object with contents of html markup in it, for example: string
I have thousands of HTML files to process using Groovy/Java and I need to
Is it possible to replace javascript w/ HTML if JavaScript is not enabled on
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have just tried to save a simple *.rtf file with some websites and

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.