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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T03:01:30+00:00 2026-05-27T03:01:30+00:00

I wanted to apply the regular expressions to find and replace the unwanted parenthesis

  • 0

I wanted to apply the regular expressions to find and replace the unwanted parenthesis and operators in the input string.

Here is the possible input for me: 4 types from a through d. [Invalid Inputs]

a). 1 and (2 or 3) ()
b). ( and 2)
c). (or 4)
d). ()

all these 4 are invalid cases , the valid ones should be as [Valid Inputs]

a). 1 and 2
b). (1 and 2)
c). 1 and (2 or 4)

Based on this requirement, i have written the regex, but i have written in 2 parts and need help in joining them to a single regex.

a). ([(]+[\s]*[)]+) -> to find the empty parenthesis
b). (([(]+[\s]*[and|or]+[\s]*)) -> to find cases like b or c in invalid inputs.

Kindly suggest a way to combine the above. further i want to do removal of the invalid parts in the inputs, which i can do in javascript like string.replace(regex).

Kindly analyze and give comments on this process.

  • 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-27T03:01:30+00:00Added an answer on May 27, 2026 at 3:01 am
    /\((\s*|\s*(?:and|or)\s*\d+\s*|\s*\d+\s*(?:and|or)\s*|\s*(?:and|or)\s*)\)/
    

    is a Regex that checks for the content of a bracket pair: either empty, missing operand on the left, missing operand on the right or no operands at all.

    But watch out! This neither checks for validity of un-bracketed expressions, nor is it rekursive, as Colin Fine already mentioned. If you liked to check for that, I’d propose to replace from inside up:

    var s = string;
    var oneoperator = /^\s*\d+\s*(and|or)\s*\d+\s*$/;
    while (true) {
        s = s.replace(/\(([^)])\)/, function(all, inner) {
            if (inner.match(oneoperator)
                return "0"; // or any other valid operand
            else
                throw new SyntaxError("Math Syntax mismatch");
        });
        if (s.match(oneoperator))
            break; // return true
    }
    // to be improved
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a branch named lemon. I wanted to pull changes into it from
I use offset1 span6 for the main content and wanted to apply the same
I am working on web application. I wanted to apply auto height to textarea
I wanted to know how to apply a constructor on each element of an
I am a newbie and wanted to know how to apply transparent color in
Before I plan to apply some algorithm, wanted to clarify, whether easier solution is
I wanted to save a string to a file and read it back, so
Possible Duplicate: how to apply a function to every row of a matrix (or
I'm having a problem when trying to apply a regular expression to some strings
The question whether it is possible to apply multiple classes/styles to an element in

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.