Sphere
Log in Join
☆ 3

esbuild minify breaks a regex with a unicode property

Vite 5 uses esbuild for minify in prod. I have const re = /\p{L}+/gu in a validation util. In dev it works. In the minified build it throws: SyntaxError: Invalid regular expression: /\p{L}+/gu: Invalid escape, or sometimes just matches nothing. I added target: 'es2020' to build config and it helped on Chrome but older Safari still complains. What is the right target for unicode property escapes?

⚑ Report

4 replies

☆ 0
Y yaml ·2026-10-16#1

Unicode property escapes need ES2018+ target. If esbuild transpiles the regex down or leaves it but your target is too low, the output can be mangled. Set build.target to es2018 or higher and also check you are not running esbuild directly elsewhere with a lower target. What does the emitted line look like?

⚑ Report
☆ 0
L loki ·2026-10-17#2

The emitted line had the backslash stripped, it was /p{L}+/gu, missing the backslash entirely. Turned out my source had a single backslash in a JS string context somewhere upstream that got eaten before esbuild saw it. Fixing the source escaping resolved it, the target was a red herring.

⚑ Report
☆ 0
P plastic ·2026-10-19#3

Ah, double-escaping. In a string you need '\\p{L}' with two backslashes so the actual regex source has one. If you are constructing with new RegExp('\\p{L}+', 'gu') the string literal must escape the backslash. Easy to miss when copying from a regex literal.

⚑ Report
☆ 0
P plastic64 ·2026-10-26#4

Also note unicode property escapes are unsupported in Safari before 11.1 and some old Android WebViews. If you really need old browser support, avoid \p{L} entirely and use a plain character class. We had to do that for a WebView app.

⚑ Report
Log in to reply.