regex for escaping '\{{expression}}'?

Viewed 40

I'd like to use mustache style for expression: {{abc}}, it's pretty easy to write /({{[a-z]+}})/.

However I cannot get it right to handle \{{abc}}, for which I'd like to skip them on match list. I tried /((?!\\)({{[a-z]+}}))/ but it doesn't work.

https://regex101.com/r/67nXA2/2

1 Answers

Use

(?<!\\)(?:\\\\)*\K{{[a-z]+}}

See proof

Explanation

--------------------------------------------------------------------------------
  (?<!                     look behind to see if there is not:
--------------------------------------------------------------------------------
    \\                       '\'
--------------------------------------------------------------------------------
  )                        end of look-behind
--------------------------------------------------------------------------------
  (?:                      group, but do not capture (0 or more times
                           (matching the most amount possible)):
--------------------------------------------------------------------------------
    \\                       '\'
--------------------------------------------------------------------------------
    \\                       '\'
--------------------------------------------------------------------------------
  )*                       end of grouping
--------------------------------------------------------------------------------
  \K                       match reset operator
--------------------------------------------------------------------------------
  {{                       '{{'
--------------------------------------------------------------------------------
  [a-z]+                   any character of: 'a' to 'z' (1 or more
                           times (matching the most amount possible))
--------------------------------------------------------------------------------
  }}                       '}}'
Related