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

The Archive Base Latest Questions

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

I want to write a regexp for balanced braces constructs like {…} , {…

  • 0

I want to write a regexp for balanced braces constructs like

{...}, {... {..}...} and {...{..{..}...}..{..}...} where ... means any text, which has no ‘{‘ or ‘}’ chars

If I do this:

   txt.replace(/\{[\s\S]+?}/g, function(s){return "_"+s+"_";})

the first } will be considered as end match, so
{ { } } will become _{ { }_ }

I want a regex to convert this to _{ { } }_

PS: the function(s) part is not from real code, just for example
PPS: I want to do this with regexps if it possible.
PPPS: Highest level of ‘{‘ braces included in the text is limited, to 3 or 4 usually

UPDATE:
Thanks for comments, Let’s limit the number of nested braces to 3. So:

{ { { .. } .. { .. } } .. { .. } }

  • 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-18T09:30:49+00:00Added an answer on May 18, 2026 at 9:30 am

    Thanks for comments, Let’s limit the number of nested braces to 3.

    OK, that gives something that we can do with regular expressions. (There are such things as “extended” regular expressions, such as found in Perl or Python, that can match arbitrary nested braces, by using something called “backreferences”. I don’t know if Javascript’s “regular expressions” support those, though.)

    We build up the regular expression in pieces.

    First, let’s figure out what a chunk with no nested braces looks like:

    [^{}]*
    

    OK, that was easy. Any number of non-brace characters. 🙂

    A construct with one level of braces, therefore, looks like

    {[^{}]*}
    

    since we want braces at the beginning and end, and nowhere else.

    How about up to 2 levels?

    Well, then we still have braces at beginning and end, and our content can be “any number of chunks of data, each of which has either 0 or 1 levels of braces”.

    So, we put together “0 or 1 levels of braces” by joining those two expressions with | (so that we match one or the other), and put parentheses around that (because we want to treat that as a whole unit) and a * afterward to denote “any number of this thing we just defined between the parentheses”. Then braces go around the whole thing. That gives:

    {({[^{}]*}|[^{}]*)*}
      ^^^^^^^^ ^^^^^^
    exp. for   exp. for
    1 level    0 levels
    

    3 levels of braces are left as an exercise. 🙂 Hint: We apply the same logic – braces enclose any number of chunks, each of which has up to 2 levels of braces.

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

Sidebar

Related Questions

Is there a library that would let me write regexp-like queries for lists of
I have text file with several thousands lines. I want to parse this file
I want to write a function that allows users to match data based on
Ok I give up, I've been trying to write a regexp in ant to
I try to write a simple Markdown parser in JavaScript. Therefore I want to
I'm trying to write a regular expression for my html parser. I want to
I write a simple program to print a image in JSF.... I have one
I'm stuck with a regular expression problem. I've got a string which I need
I have an text that consists of information enclosed by a certain pattern. The
I have the following situation: There is a tool that gets an XSLT from

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.