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

127
Vistas
How to extract content in between an opening and a closing bracket?

I am trying to split a string into an array of text contents which each are present within the [@ and ] delimiters. Just characters in between [@and ] are allowed to match. Being provided with a string like ...

const stringA = '[@Mary James], [@Jennifer John] and [@Johnny Lever[@Patricia Robert] are present in the meeting and [@Jerry[@Jeffery Roger] is absent.'

... the following result is expected ...

[
  'Mary James',
  'Jennifer John',
  'Patricia Robert',
  'Jeffery Roger'
]

Any logic which leads to the expected outcome can be used.

A self search for a solution brought up the following applied regex ...

stringA.match(/(?<=\[@)[^\]]*(?=\])/g);

But the result doesn't fulfill the requirements because the array features the following items ...

[
  'Mary James',
  'Jennifer John',
  'Johnny Lever[@Patricia Robert',
  'Jerry[@Jeffery Roger'
]
about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

Close, just missing the exclusion of [:

stringA.match(/(?<=\[@)[^\[\]]*(?=\])/g);
//                       ^^ exclude '[' as well as ']'
about 4 years ago · Juan Pablo Isaza Denunciar

0

The OP's regex does not feature the opening bracket within the negated character class, thus changing the OP's /(?<=\[@)[^\]]*(?=\])/g to (?<=\[@)[^\[\]]*(?=\]) already solves the OP's problem for most environments not including safari browsers due to the lookbehind which is not supported.

Solution based on a regex ... /\[@(?<content>[^\[\]]+)\]/g ... with a named capture group ...

const sampleText = '[@Mary James], [@Jennifer John] and [@Johnny Lever[@Patricia Robert] are present in the meeting and [@Jerry[@Jeffery Roger] is absent.'

// see ... [https://regex101.com/r/v234aT/1]
const regXCapture = /\[@(?<content>[^\[\]]+)\]/g;

console.log(
  Array.from(
    sampleText.matchAll(regXCapture)
  ).map(
    ({ groups: { content } }) => content
  )
);

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