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

173
Vistas
Split string with ignore special RegEx substring

I need split the string, split character is "," but must skip content in "("...")".

const paramString = "gyro (param1, param2), proximity, Ultra Wideband (UWB) support, compass";

const paramArr = paramString.match(/[^(\)\,]+(\(.+?\))?/g).map( item => item.trim() ); 

In my result is paramArr[2] bad.

[ "gyro (param1, param2)", "proximity", "Ultra Wideband (UWB)", "support", "compass" ]

I need result:

[ "gyro (param1, param2)", "proximity", "Ultra Wideband (UWB) support", "compass" ]

Please advise how update my RegEx.

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

You should use negative lookahead. You are basically selecting all commas (with spaces after them to trim them) that are not followed by a ) if there is no ( before it:

const paramString = "gyro (param1, param2), proximity, Ultra Wideband (UWB) support, compass";
const paramArr = paramString.split(/,\s*(?![^(]*\))/);

console.log(paramArr);

NOTE: it won't work for nested parentheses. It will fail in the following scenario:

const paramString = "gyro (param1, (param2)), proximity, Ultra Wideband (UWB) support, compass";
about 4 years ago · Juan Pablo Isaza Denunciar

0

Try below mentioned regx , this will break your string in expected tokens only.

/[\w\ ]+(\([^)]*\))?(\ \w+)?/gi

The regx breaks your string in following tokens .

gyro (param1, param2)
 proximity
 Ultra Wideband (UWB) support
 compass

Please follow the link for live demo of the regx :

https://regex101.com/r/yCYHgw/1

https://regex101.com/r/yCYHgw/2


(updated, regx to work only with non nested )

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