3

example string:

hello ! < and ! world>, and ! letter and [another ! letter] here.

Let's say, I want to split the sentence by ! character if it is not between < and >and also not between { and }.

I have been using :

str.split(/\!+(?=(?:(?:[^<]*"){2})*[^>]*$)/g);

to split if not between < and > , but how to add the another clause { and } too? Putting | doesnt solve, because it might then match blended < and }..

T.Todua
  • 53,146
  • 19
  • 236
  • 237
  • 1
    Assuming your reger works, just duplicate with the second delimiter and put an or `|` in between? – Griffin Jun 27 '20 at 22:07

1 Answers1

1

/!(?![^<]*>)(?![^{]*\})/g

!(?![^<]*>) matches a ! if it is not followed followed by a >, unless it is preceeded by a <. Gotten from this answer

Then simply chained with another negative lookahead for the second set of delimiters.

Demo here

Griffin
  • 13,184
  • 4
  • 29
  • 43