diff --git a/Userland/Libraries/LibJS/Runtime/StringPrototype.cpp b/Userland/Libraries/LibJS/Runtime/StringPrototype.cpp index 4e42a7b786..5dc50cbcc5 100644 --- a/Userland/Libraries/LibJS/Runtime/StringPrototype.cpp +++ b/Userland/Libraries/LibJS/Runtime/StringPrototype.cpp @@ -81,6 +81,7 @@ void StringPrototype::initialize(GlobalObject& global_object) define_native_function(vm.names.match, match, 1, attr); define_native_function(vm.names.matchAll, match_all, 1, attr); define_native_function(vm.names.replace, replace, 2, attr); + define_native_function(vm.names.search, search, 1, attr); define_native_function(vm.names.anchor, anchor, 1, attr); define_native_function(vm.names.big, big, 0, attr); define_native_function(vm.names.blink, blink, 0, attr); @@ -802,6 +803,28 @@ JS_DEFINE_NATIVE_FUNCTION(StringPrototype::replace) return js_string(vm, builder.build()); } +// 22.1.3.19 String.prototype.search ( regexp ), https://tc39.es/ecma262/#sec-string.prototype.search +JS_DEFINE_NATIVE_FUNCTION(StringPrototype::search) +{ + auto this_object = require_object_coercible(global_object, vm.this_value(global_object)); + if (vm.exception()) + return {}; + auto regexp = vm.argument(0); + if (!regexp.is_nullish()) { + if (auto* searcher = get_method(global_object, regexp, vm.well_known_symbol_search())) + return vm.call(*searcher, regexp, this_object); + if (vm.exception()) + return {}; + } + auto s = this_object.to_string(global_object); + if (vm.exception()) + return {}; + auto rx = regexp_create(global_object, regexp, js_undefined()); + if (!rx) + return {}; + return rx->invoke(vm.well_known_symbol_search(), js_string(vm, s)); +} + // B.2.3.2.1 CreateHTML ( string, tag, attribute, value ), https://tc39.es/ecma262/#sec-createhtml static Value create_html(GlobalObject& global_object, Value string, const String& tag, const String& attribute, Value value) { diff --git a/Userland/Libraries/LibJS/Runtime/StringPrototype.h b/Userland/Libraries/LibJS/Runtime/StringPrototype.h index e8ec94cc61..e40706e11a 100644 --- a/Userland/Libraries/LibJS/Runtime/StringPrototype.h +++ b/Userland/Libraries/LibJS/Runtime/StringPrototype.h @@ -46,6 +46,7 @@ private: JS_DECLARE_NATIVE_FUNCTION(match); JS_DECLARE_NATIVE_FUNCTION(match_all); JS_DECLARE_NATIVE_FUNCTION(replace); + JS_DECLARE_NATIVE_FUNCTION(search); JS_DECLARE_NATIVE_FUNCTION(anchor); JS_DECLARE_NATIVE_FUNCTION(big); JS_DECLARE_NATIVE_FUNCTION(blink);