Explore Bato Web Agency Webflow Certified Partner Profile Explore Bato Web Agency Webflow Certified Partner Profile

SyntaxError: Unterminated character class in regular expression — what is wrong?

Illustration without a description Illustration without a description

A regular expression opened a character class with [ and did not reach a valid closing ]. Look at the regex itself if it is a literal. For a dynamic pattern, print the final string before calling RegExp, because the string-building step can change what the regex engine sees.

 

Check for an escape before the bracket that should close the class. If the bracket is meant to be matched literally, escape it in the right part of the expression. Keep user-supplied text separate from pattern syntax unless it has been escaped for that use.

 

const pattern = /[a-z0-9]+/i; console.log(pattern.test('A1'));

 

Place + after the closing bracket when you mean to repeat the whole character class. If the failing pattern is assembled from fragments, test those fragments separately and then inspect the combined result. A bracket can be lost when one fragment supplies only part of a class.

 

Print the final pattern before compilation and match the brackets in that string, including any escapes.