-2

A regular expression which works correctly in Chrome fails with this error message in Firefox:

SyntaxError: invalid regexp group

The regexp looks like this: /(?<=\{\{)[a-zA-Z0-9_]+(?=\}\})/g

I want to get all chars between {{ }}.

Why doesn't this work in Firefox?

Herohtar
  • 5,347
  • 4
  • 31
  • 41
Przemysław Zamorski
  • 741
  • 2
  • 14
  • 31
  • Can you show us some code? – Liora Haydont Apr 11 '18 at 20:37
  • 2
    Lookbehinds only work in Chrome 62+ right now. You're going to have to use a capture group instead `{{(\w+)}}` – ctwheels Apr 11 '18 at 20:37
  • 1
    This question is already asked [RegExp works in Chrome but not in Firefox or IE11](https://stackoverflow.com/questions/48618345/regexp-works-in-chrome-but-not-in-firefox-or-ie11) – revo Apr 11 '18 at 20:45
  • By the way, you can use a [tool like this](https://regexr.com/) to test your regular expressions. In this case it would have told you: *ERROR: The "positive lookbehind" feature is not supported in this flavor of RegEx.* – Herohtar Apr 11 '18 at 20:52

1 Answers1

2

The reason your regex (?<=\{\{)[a-zA-Z0-9_]+(?=\}\}) doesn't work in Firefox is due to the use of a lookbehind (?<=\{\{). At the moment, the ECMA TC39 proposal that includes the RegExp Lookbehind Assertions has only been implemented in one major browser (Chrome 62+). Until it's implemented in another major web browser, it is not considered mandatory for all major browsers to support (as per my understanding, although I haven't been able to find definitive proof of this behaviour). It's currently Stage 4 (Enquiry Stage).

A workaround for this? For your usage, you can simply use a capture group as the following shows:

{{(\w+)}}
ctwheels
  • 21,901
  • 9
  • 42
  • 77