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

129
Vistas
How can I split a string via first level square brackets?

I have a string like this:

key[type=order][0].item[id={getVal(obj[type=item][0].id)}]

and I want to split this string as like:

 [
     ['key', '[type=order]', '[0]'],
     ['item', '[id={getVal(obj[type=item][0].id)}]']
 ]

Firstly I split string via first level dot:

const str = 'key[type=order][0].item[id={getVal(obj[type=item][0].id)}]';
const arr = str.split(/\.(?![^[]*])/).filter(Boolean);

This generated array like this:

['key[type=order][0]', 'item[id={getVal(obj[type=item][0].id)}]']

And after that I want to split all array item via first level square bracket with:

arr.map(item => item.split(/(\[.+?\])/).filter(Boolean))

This generated array:

[
    ['key', '[type=order]', '[0]'],
    ['item', '[id={getVal(obj[type=item]', '[0]', '.id)}]']
]

I want to split items only first level square brackets not nested. How can I do this with regex in javascript?

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

0

this should do it on your final split

arr.map(item => item.split(/(\[[^\]]+\(.*\).*\])|(\[.+?\])/).filter(Boolean))

Which generates the array:

[
    ['key', '[type=order]', '[0]'],
    ['item', '[id={getVal(obj[type=item][0].id)}]']
]

The regex is /(\[[^\]]+\(.*\).*\])|(\[.+?\])/ and the first part of that looks for any square brackets with parentheses inside, and discounts any square brackets within those. After the | (or) your original regex runs!

Update

arr.map(item => item.split(/(\[[^\]]+\([^\)]*\)[^\]]*\])|(\[.+?\])/).filter(Boolean))
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