fjordtools / Errors / Regex: Unterminated group

SyntaxError: Invalid regular expression: Unterminated group

The pattern opens a group with a left parenthesis that never closes, so the engine reaches the end of the pattern still inside the group. A stray right parenthesis gives the matching error Unmatched ). Count the pairs, and escape a literal parenthesis with a backslash.

Fjordtools team · Seen in Chrome and Node (V8) · Published · 1 min read

How browsers word it

Chrome and Node (V8)Invalid regular expression: /(abc/: Unterminated group
FirefoxSyntaxError: unterminated parenthetical
SafariSyntaxError: Invalid regular expression: missing )

Common causes

  1. A group was never closed

    A left parenthesis opens a group and every group needs its closing one.

    Input

    (abc

    Regex Tester says

    Invalid regular expression: /(abc/: Unterminated group
  2. An extra right parenthesis

    A closing parenthesis with no group to close is reported as unmatched.

    Input

    abc)

    Regex Tester says

    Invalid regular expression: /abc)/: Unmatched ')'
  3. A square bracket was never closed

    A character class has the same problem with its brackets.

    Input

    [a-z

    Regex Tester says

    Invalid regular expression: /[a-z/: Unterminated character class

How to fix it

  1. Count the opening and closing parentheses, and indent nested groups while you read.
  2. Escape a literal parenthesis or bracket with a backslash: \( and \).
  3. When you build the pattern from user input, escape the input first.
  4. Paste the pattern into the regex tester to see the exact error.

Related errors

More error explainers are coming

See all errors.