-1

I want to split a string by every character including spaces except characters with parenthesis in them (both open and closed). An example:

var x = "dmk  (Xme) ww* 0 (u9*)";
var arr = [];
//code to split variable x
console.log(arr); /* returns ["d", "m", "k", "", "", "(Xme)", "", "w", "w", "*", "", "0", "", "(u9*)"]

I'm assuming you would have to use regex and the split function but I'm terrible with regex no matter how much I try.

adiga
  • 34,372
  • 9
  • 61
  • 83

1 Answers1

2

We can try splitting on the following negative lookahead:

(?![^(]*\))

This will only split on content which is not inside parentheses.

var x = "dmk  (Xme) ww* 0 (u9*)";
var parts = x.split(/(?![^(]*\))/);
console.log(parts);
Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360