Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

92
Views
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 answers
Answer question

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 Report

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!