Repository navigation
Description
\p{…} / \P{…} names are looked up in LazyMap (src/UnicodeTables.js) with key in this.initializer and this.initializer[key] on a plain object literal, so names inherited from Object.prototype are treated as Unicode classes. RE2 (and an unknown name such as \p{NoSuchClass}) gives a parse error instead.
Reproduction (re2js 2.9.1, Node 26)
import { RE2JS } from 're2js'
for (const p of ['\\p{NoSuchClass}', '\\p{constructor}', '\\p{isPrototypeOf}', '\\P{constructor}',
'\\p{toString}', '\\p{valueOf}', '\\p{hasOwnProperty}', '\\p{__proto__}']) {
try { console.log(p, 'compiled; test("abc") =', RE2JS.compile(p).test('abc')) }
catch (e) { console.log(p, `${e.constructor.name}: ${e.message}`) }
}Output:
\p{NoSuchClass} RE2JSSyntaxException: error parsing regexp: invalid character class range: `\p{NoSuchClass}`
\p{constructor} compiled; test("abc") = false
\p{isPrototypeOf} compiled; test("abc") = false
\P{constructor} compiled; test("abc") = true
\p{toString} TypeError: table.getLo is not a function
\p{valueOf} TypeError: Cannot convert undefined or null to object
\p{hasOwnProperty} TypeError: Cannot convert undefined or null to object
\p{__proto__} TypeError: fn is not a function
Expected
All of them should throw RE2JSSyntaxException (invalid character class range), as \p{NoSuchClass} does and as RE2 does.
Impact
A pattern with such a name either compiles and silently matches nothing (or everything, for \P{…}), or fails with an internal TypeError instead of a syntax error. This matters when patterns come from configuration and a compile error is the signal that a pattern is invalid.
Possible fix
Use own-property checks in LazyMap, e.g.
has(key) {
return Object.hasOwn(this.initializer, key)
}
get(key) {
if (this.cache.has(key)) return this.cache.get(key)
const fn = Object.hasOwn(this.initializer, key) ? this.initializer[key] : undefined
const val = fn ? fn() : null
this.cache.set(key, val)
return val
}(or create the initializer objects with a null prototype).
Activity
Fix for #71 to accept only unicode groups
le0pard commentedon Nov 10, 2026
Fixed and published in
2.10.0Please checkajmtrz commentedon Nov 10, 2026
Confirmed fixed in 2.10.0: all of
\p{constructor},\p{isPrototypeOf},\P{constructor},\p{toString},\p{valueOf},\p{hasOwnProperty},\p{__proto__}and[\p{constructor}]now throwRE2JSSyntaxException: invalid character class range, the same as\p{NoSuchClass}. Thanks for the quick fix!