From f406d6f78c0dfe589236b8a19e91e0e403009805 Mon Sep 17 00:00:00 2001 From: Ben Noordhuis Date: Sat, 2 Mar 2024 13:29:15 +0100 Subject: [PATCH] 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 --- libregexp.c | 3 +++ tests/test_builtin.js | 3 +++ 2 files changed, 6 insertions(+) diff --git a/libregexp.c b/libregexp.c index c2f9a1f..64213fc 100644 --- a/libregexp.c +++ b/libregexp.c @@ -783,6 +783,9 @@ static int get_class_atom(REParseState *s, CharRange *cr, /* always valid to escape these characters */ goto normal_char; } else if (s->is_unicode) { + // special case: allowed inside [] but not outside + if (ret == -2 && *p == '-' && inclass) + goto normal_char; invalid_escape: return re_parse_error(s, "invalid escape sequence in regular expression"); } else { diff --git a/tests/test_builtin.js b/tests/test_builtin.js index 93e2490..2428c69 100644 --- a/tests/test_builtin.js +++ b/tests/test_builtin.js @@ -699,6 +699,9 @@ function test_regexp() ex = _ex; } assert(ex?.message, "invalid class range"); + + eval("/[\\-]/"); + eval("/[\\-]/u"); } function test_symbol()