0

I have this regex, it's an exemple:

/par(tir|te|s)|(quitter.*maison)|((heure|quand).*(rendez-vous|journée))/i

I would like to split it by "|" in a regular expression but this character can't be inside parenthesis for the explode. So the expected result have to be that:

1: par(tir|te|s)
2: (quitter.*maison)
3: ((heure|quand).*(rendez-vous|journée))

My mind is in trouble, someone can help me?

Yuankun
  • 6,875
  • 3
  • 32
  • 34

3 Answers3

0

With a huge help from this old question, I was able to construct this regex.
It matches parenthesis with their content, including preceding text:

(?:\w+)?\((?:[^()]*|\((?:[^()]*|\([^()]*\))*\))*\)

However I must say that nested parenthesis are super hard to handle with regular expressions.

GalAbra
  • 5,048
  • 4
  • 23
  • 42
0

You can split on this regex, which achieves precisely the output you specified from the input sample provided:

/\|(?!(?:\w+\|?)+\))/ig
0

You can try this regex /\|(?![^\(]*\))/g

const str = '/par(tir|te|s)|(quitter.maison)|((heure|quand).(rendez-vous|journée))/i';
console.log(str.split(/\|(?![^\(]*\))/g));
Hassan Imam
  • 21,956
  • 5
  • 41
  • 51