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

257
Views
How to match regex with conditions?

"Fingerprint (rear-mounted, region dependent), accelerometer, proximity"

Above is the string of sensors in a device. I'm trying to match every sensor. The problem is that when I match above using /.+?,/g, The matches are:

  1. Fingerprint (rear-mounted,
  2. region dependent),
  3. accelerometer,
  4. proximity

I'm getting 4 sensors here instead of three. Because, of course, my pattern matches these. What I want to do is that when a , is preceded by (, then I want to match ), and when not preceded by (, I want to match ,

I think I need to use lookarounds here. But I can't get them to work for me. I tried multiple regexes. It's silly to paste them all here :)

I'm using JavaScript

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

0

Here is one way to do so:

(?:[^(,]+(?:\([^)]*\))?)+?(?:, |$)

See the online demo here.

const pattern = /(?:[^(,]+(?:\([^)]*\))?)+?(?:, |$)/g;
const str = 'Fingerprint (rear-mounted, region dependent), accelerometer, proximity';

const matches = str.matchAll(pattern)
for (const match of matches) {
  console.log(match[0]);
}


  • (?:)+?: Non capturing group, repeated between one and unlimited times, as few as possible.
    • [^(,]: Matches any character other than ( or ,.
    • (?:)?: Optional non capturing group.
      • \(: Matches (.
      • [^)]*: Matches any character other than ), between zero and unlimited times, as much as possible.
      • \): Matches ).
  • (?:): Non capturing group.
    • , : Matches , .
    • |: Or.
    • $: Matches the end of the string.
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!