Short answer
Look at the pattern in the message. If it came from user input or a filename, escape it before building the RegExp: RegExp.escape(text) in current browsers and recent Node.js, or a replace() fallback for older ones. If it is a form field’s pattern attribute, remember browsers compile it with the v flag, so a hyphen inside [ ] must be escaped as \- . For Invalid escape, remove backslashes that do not mean anything in u or v mode.
Why the pattern is invalid, and the fix
01
User input or a filename used as a pattern
“When dynamically creating a RegExp with user-provided content, consider using this function to sanitize the input (unless the input is actually intended to contain regex syntax).” MDN, RegExp.escape()
new RegExp(search) breaks as soon as someone searches for +1 (555) or *.js, because + * ( and ? are regex syntax. Escape the text first. RegExp.escape is Baseline 2025, so keep a fallback if you support older browsers.
const pattern = RegExp.escape ? RegExp.escape(search) : search.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); const re = new RegExp(pattern, 'i');02
A pattern attribute that the v flag rejects
“No regular expression is applied if the attribute value is absent, an empty string, or invalid.” MDN, HTML attribute: pattern
pattern="[a-z0-9-_]+" was fine for years, but browsers now compile pattern with the v flag, where a hyphen inside a character class must be escaped. The browser logs the error and stops checking that field, so bad input gets through.
<!-- before: invalid with the v flag --> <input name="username" pattern="[a-z0-9-_]+"> <!-- after --> <input name="username" pattern="[a-z0-9_\-]+">03
An unneeded backslash in u or v mode
“This severely limits the ability to add new escape sequences in the future, so in Unicode-aware mode, only recognized escape sequences are allowed. Do not add \ redundantly.” MDN, SyntaxError: invalid identity escape in regular expression
Without a flag, \- or \_ just mean the character itself. With the u or v flag, every escape must mean something, so /\-/u throws Invalid escape. Drop the extra backslash, or escape only characters that need it.
// throws: Invalid escape /\-/u; // works /-/u;04
A group that is not closed, or syntax from another language
“Maybe you are using a feature that is not supported by the current JavaScript engine.” MDN, SyntaxError: invalid regexp group
Unterminated group means a ( has no matching ). Invalid group means (? is followed by something JavaScript does not support, such as Perl or PCRE syntax copied from another tool. Safari before 16.4 rejects lookbehind (?<=...), so a pattern can work in Chrome and fail on an older iPhone.
Checked against the vendor’s docs on : MDN: RegExp.escape(); MDN: HTML attribute: pattern; MDN: SyntaxError: nothing to repeat; MDN: SyntaxError: invalid identity escape; MDN: SyntaxError: invalid regexp group.
Rather hand it over?
Fixed scope, fixed price, paid upfront. The work happens in writing; no call needed.
- from $450App Fix TicketOne reproducible bug in an existing mobile or web app, diagnosed and fixed.See scope and start
- from $450AI-Built App RescueA Lovable, Bolt or Cursor app checked end to end in 3 days, then its worst problems fixed at a fixed price.No issues worth fixing, no charge. If the Launch Check finds nothing rated medium or higher, Simon refunds the full $450. You keep the report either way. This applies to the first 10 Launch Checks bought after October 30, 2026.See scope and start
Related: “Invalid or unexpected token” · “Unexpected identifier” · “undefined is not an object”