pelican_george pelican_george - 5 months ago 12
Javascript Question

Javascript REGEX to match multiple custom tags (also incomplete)

I'm trying to match "custom" tags that might be complete/incomplete as described below.

The bold text is what I'm trying to match.


  1. %end{some text

  2. %start{some text

  3. %start{some text}%end

  4. %start{some text}%end%start{more text}%end



Also, these tags can appear multiple times within a string. For example, the regex:

/%start(.*)%end/gi


applied on the 4th example would capture:
%start{some text}%end%start{more text}%end

How would I go on about tho achieve the matches described on the first 4 examples?

Answer

With a lookbehind :

/(?<=%start{|%end{)[^}]+/g

With the content in the first group :

/%(?:start|end){([^}]+)/g

You may need to add \r and \n to the negated character classes if you handle the data multiple lines at a time, as in my regex101 tests (lookbehind, first group).

Comments