diff --git a/Userland/Libraries/LibJS/Runtime/RegExpObject.cpp b/Userland/Libraries/LibJS/Runtime/RegExpObject.cpp index b3f49a2ca7..ed54cee572 100644 --- a/Userland/Libraries/LibJS/Runtime/RegExpObject.cpp +++ b/Userland/Libraries/LibJS/Runtime/RegExpObject.cpp @@ -39,8 +39,8 @@ static Flags options_from(const String& flags, VM& vm, GlobalObject& global_obje Flags options { // JS regexps are all 'global' by default as per our definition, but the "global" flag enables "stateful". // FIXME: Enable 'BrowserExtended' only if in a browser context. - { (regex::ECMAScriptFlags)regex::AllFlags::Global | ECMAScriptFlags::BrowserExtended }, - {}, + .effective_flags = { (regex::ECMAScriptFlags)regex::AllFlags::Global | (regex::ECMAScriptFlags)regex::AllFlags::SkipTrimEmptyMatches | regex::ECMAScriptFlags::BrowserExtended }, + .declared_flags = {}, }; for (auto ch : flags) { diff --git a/Userland/Libraries/LibJS/Runtime/RegExpPrototype.cpp b/Userland/Libraries/LibJS/Runtime/RegExpPrototype.cpp index d1c1886dce..f808400ee7 100644 --- a/Userland/Libraries/LibJS/Runtime/RegExpPrototype.cpp +++ b/Userland/Libraries/LibJS/Runtime/RegExpPrototype.cpp @@ -1,6 +1,6 @@ /* * Copyright (c) 2020, Matthew Olsson - * Copyright (c) 2020, Linus Groh + * Copyright (c) 2020-2021, Linus Groh * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -194,10 +194,9 @@ JS_DEFINE_NATIVE_FUNCTION(RegExpPrototype::exec) for (size_t i = 0; i < result.n_capture_groups; ++i) { auto capture_value = js_undefined(); - if (result.capture_group_matches[0].size() > i) { - auto& capture = result.capture_group_matches[0][i]; + auto& capture = result.capture_group_matches[0][i + 1]; + if (!capture.view.is_null()) capture_value = js_string(vm, capture.view.to_string()); - } array->indexed_properties().put(array, i + 1, capture_value); } diff --git a/Userland/Libraries/LibJS/Tests/builtins/RegExp/RegExp.prototype.exec.js b/Userland/Libraries/LibJS/Tests/builtins/RegExp/RegExp.prototype.exec.js index 3cdd5dd8e3..4ec24c1645 100644 --- a/Userland/Libraries/LibJS/Tests/builtins/RegExp/RegExp.prototype.exec.js +++ b/Userland/Libraries/LibJS/Tests/builtins/RegExp/RegExp.prototype.exec.js @@ -28,6 +28,16 @@ test("basic unnamed captures", () => { expect(res[2]).toBe(undefined); expect(res.groups).toBe(undefined); expect(res.index).toBe(0); + + re = /(foo)?(bar)/; + res = re.exec("bar"); + + expect(res.length).toBe(3); + expect(res[0]).toBe("bar"); + expect(res[1]).toBe(undefined); + expect(res[2]).toBe("bar"); + expect(res.groups).toBe(undefined); + expect(res.index).toBe(0); }); test("basic named captures", () => {