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

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

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

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

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 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