Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

164
Views
How to match everything outside of brackets and everything inside of brackets?

These are some demo strings,

enum('a', 'b', 'c')
varchar(255)
int unsigned

Taking first one as an example, I want to match enum and 'a', 'b', 'c'. Everything inside brackets are optional as you see in 3rd example: int unsigned. How do I do this?

I tried,

(.*)(\((.*)\))?

However this matches whole string for some reason.

const out = /(.*)(\((.*)\))?/.exec(`enum('a', 'b', 'c')`)
console.log(out)

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

You can use

const [, first, second] = /^(.*?)(?:\((.*)\))?$/.exec(`enum('a', 'b', 'c')`)
console.log(first + '\n' + second)

See the regex demo. Details:

  • ^ - start of string
  • (.*?) - Group 1: any zero or more chars other than line break chars as few as possible
  • (?:\((.*)\))? - an optional sequence of (, any zero or more chars other than line break chars as many as possible (captured into Group 2), and then a ) char
  • $ - end of string.
about 4 years ago · Juan Pablo Isaza Report

0

const tests = [
    "enum('a', 'b', 'c')",
    "varchar(255)",
    "int unsigned"
];

tests.forEach(test => console.log(/([^(]+)(?:\((.*)\))?/.exec(test)));

The first capturing group grabs the part before the parenthesis, the second optionally what is contained within the parenthesis if applicable.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!