Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

261
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda