Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex - split if not inside the two characters

Tags:

javascript

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 }..

like image 636
T.Todua Avatar asked Jul 04 '26 13:07

T.Todua


1 Answers

/!(?![^<]*>)(?![^{]*\})/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

like image 161
Griffin Avatar answered Jul 07 '26 10:07

Griffin