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

93
Vistas
Regex split string at specific set of characters

Suppose I have a string of this nature Set 1 (2) Set 2 (2) Set 3 (2) Set 4 (2) [Choose Two]. How can I make a regex that starts after (important that it's after or I can just add it back) every ) character and optionally ends at a ] character, so splitting the string would look something like this?

Set 1 (2) Set 2 (2) Set 3 (2) Set 4 (2) [Choose Two]
-> [Set 1 (2)], [Set 2 (2)], [Set 3 (2)], [Set 4 (2)]

There might be some empty characters and trailing whitespaces in the created array when using regex but I can remove that. My current try is something like /\)(\s+).+(\]?)/gm but due to the .+ being greedy It goes all the way to the end for each match like so:

Set 1 (2) Set 2 (2) Set 3 (2) Set 4 (2) [Choose Two]
-> Set 1 (2{) Set 2 (2) Set 3 (2) Set 4 (2) [Choose Two]}
-> [Set 1 (2]

It also includes the ) when splitting which is undesirable.

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

0

Try:

(?<!\S).*?\)
\b.*\)

See an online demo


  • (?<!\S) = Negative lookbehind to assert position is not preceded by a non-whitespace char;
  • .*?\) - 0+ (lazy) characters upto a literal closing paranthesis.

The difference with the alternative \b.*\) is to use a word-boundary, but I'm not sure if that goes well with your actual data.


var s = 'Set 1 (2) Set 2 (2) Set 3 (2) Set 4 (2) [Choose Two]';
var m = s.match(/(?<!\S).*?\)/g);
console.log(m)

about 4 years ago · Juan Pablo Isaza Denunciar

0

const s = "Set 1 (2) Set 2 (2) Set 3 (2) Set 4 (2) [Choose Two]";
const m = s.match(/[^)]+(?:\))/g);
console.log(m)

To handle the leading whitespace:

const s = "Set 1 (2) Set 2 (2) Set 3 (2) Set 4 (2) [Choose Two]";
const m = s.match(/(?<= |^)[^)]+(?:\))/g);
console.log(m)

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

about 4 years ago · Juan Pablo Isaza Denunciar

0

The simplest would be to use String.split with this regex:

/(?<=\))/g

It simply looks behind for a ) and then splits the text into an array.

However, you then need to remove the last item in the array (which will contain [Choose Two]).

Then you have array with each Set.

You can play with it here.

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