-1

I am finding difficulty in splitting an string in JavaScript(jquery) by a closing bracket and have to keep the delimiter that is used to split.

 ex: (GROUP=test1)(GROUP=test2)(GROUP=test3)(GROUP=test4)

needs to be split by using ) and keep the delimiter with it

o/p:

(GROUP=test1)
(GROUP=test2)
(GROUP=test3)
(GROUP=test4)

i thought of a workaround of replacing ')' with '),' and then split by ',' which will solve my issue..but wanted to do in a clean way..

any help is greatly appreciated..

K Scandrett
  • 16,390
  • 4
  • 40
  • 65
marc
  • 319
  • 1
  • 5
  • 20

1 Answers1

1
var source = '(GROUP=test1)(GROUP=test2)(GROUP=test3)(GROUP=test4)';
var results = source.match(/\([^)]*?\)/g);

gives the requisite array in results.

The regex translates to: (, followed by the shortest possible series of characters not including ), followed by ); return multiple matches

Reference:

Ouroborus
  • 16,237
  • 4
  • 39
  • 62