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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T14:22:07+00:00 2026-05-22T14:22:07+00:00

I am trying to find a regular expression which will do the following (working

  • 0

I am trying to find a regular expression which will do the following (working in Javascript). I want to take a string which contains some tokens like (token) inside parentheses. My aim is to capture the tokens (including the parentheses). I will assume that parenthese are not nested, and that every open parenthesis is eventually closed.

The regular expression I would use is

[[^\(\)]*|(\(.*?\))]*

Let me break it down:

[            # Either of two things:
  [^\(\)]*   # the first is a substring not containing parentheses
|
  (          # the second is to be captured...
    \(.*?\)  # and should contain anything in parentheses - lazy match
  )
]*           # Any number of these blocks can appear

Needless to say, this will not work (why would I be asking here otherwise?):

var a = /[[^\(\)]*|(\(.*?\))]*/;
a.exec('foo(bar)');

It fails both in Firefox and Node. My previous attempt was a slightly more compicated regex:

(?:[^\(\)]*(\(.*?\)))*[^\(\)]*

which can be described as follows

(?:              # A non-capturing group...
  [^\(\)]*       # ...containing any number of non-parentheses chars
  (\(.*?\))      # ...followed by a captured token inside parentheses.
)*               # There can be any number of such groups
[^\(\)]*         # Finally, any number of non-parentheses, as above

This will work on foo(bar), but will fail on foo(bar)(quux), catpuring only quux.

How should I fix the above regex?

  • 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-22T14:22:08+00:00Added an answer on May 22, 2026 at 2:22 pm

    You can’t have an arbitrary amount of capture groups in a regex. use the /g flag to accomplish this instead: s.match(/\([^\)]+\)/g)

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

Sidebar

Related Questions

I am trying to create a JavaScript regular expression that can find a keyword
I'm trying to find a regular expression for a URL that will be compliant
I'm trying to find a regular expression that matches a word in a string
I'm having problems creating a regular expression which will fix a valid string. The
I am trying to a regular expression which extracs the data from a string
I'm trying to find a single regular expression that I can use to parse
Im trying to find a regular expression that matches this kind of URL: http://sub.domain.com/selector/F/13/K/100546/sampletext/654654/K/sampletext_sampletext.html
I'm trying to write a regular expression which should be able to identify boolean
I want to match a portion of a string using a regular expression and
I am trying to find a word with regular expression, but i don't know

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.