Modify this regex: {([^ \]] *)} to accept \]

I have this regex:

{([^\]]*)} // any character except ']' but i want it to accept also '\]' this combination of 2chars

      

Example

'Lorem Ipsum is simply] dummy text' should return => 'Lorem Ipsum is simply' (and this ones does) but
'Lorem Ipsum is simply\] dummy text' => should return all the text because the ']' is escaped

      

hope it makes sense

+3


source to share


2 answers


You can use alternation:



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

      

+7


source


I would go with:

'~(?:[^\\\\\\]]*|\\\\.)*~' // (?:[^\\\]]|\\.)*

      



Contrary to Marks answer, this allows for parsing [\\]]

as \\

, which will give you the ability to use \

as a universal escape character for more than \]

.

0


source







All Articles