Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

177
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda