Accept /[\-]/u as a valid regular expression (#288)

The non-Unicode version of the pattern was already accepted.

test262 tests it in an inverted sense in
test/built-ins/RegExp/unicode_restricted_identity_escape.js but
it appears to be per spec and both V8 and Spidermonkey accept it.

Fixes: https://github.com/quickjs-ng/quickjs/issues/286
This commit is contained in:
Ben Noordhuis 2024-03-02 13:29:15 +01:00 committed by GitHub
parent 7dd2868856
commit f406d6f78c
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 6 additions and 0 deletions

View file

@ -783,6 +783,9 @@ static int get_class_atom(REParseState *s, CharRange *cr,
/* always valid to escape these characters */ /* always valid to escape these characters */
goto normal_char; goto normal_char;
} else if (s->is_unicode) { } else if (s->is_unicode) {
// special case: allowed inside [] but not outside
if (ret == -2 && *p == '-' && inclass)
goto normal_char;
invalid_escape: invalid_escape:
return re_parse_error(s, "invalid escape sequence in regular expression"); return re_parse_error(s, "invalid escape sequence in regular expression");
} else { } else {

View file

@ -699,6 +699,9 @@ function test_regexp()
ex = _ex; ex = _ex;
} }
assert(ex?.message, "invalid class range"); assert(ex?.message, "invalid class range");
eval("/[\\-]/");
eval("/[\\-]/u");
} }
function test_symbol() function test_symbol()