diff --git a/Userland/Libraries/LibJS/Runtime/RegExpObject.cpp b/Userland/Libraries/LibJS/Runtime/RegExpObject.cpp index ee81432940..4352ef518c 100644 --- a/Userland/Libraries/LibJS/Runtime/RegExpObject.cpp +++ b/Userland/Libraries/LibJS/Runtime/RegExpObject.cpp @@ -10,6 +10,7 @@ #include #include #include +#include namespace JS { @@ -188,6 +189,15 @@ RegExpObject* RegExpObject::regexp_initialize(GlobalObject& global_object, Value return this; } +// 22.2.3.2.5 EscapeRegExpPattern ( P, F ), https://tc39.es/ecma262/#sec-escaperegexppattern +String RegExpObject::escape_regexp_pattern() const +{ + if (m_pattern.is_empty()) + return "(?:)"; + // FIXME: Check u flag and escape accordingly + return m_pattern.replace("\n", "\\n", true).replace("\r", "\\r", true).replace(LINE_SEPARATOR_STRING, "\\u2028", true).replace(PARAGRAPH_SEPARATOR_STRING, "\\u2029", true).replace("/", "\\/", true); +} + // 22.2.3.2.4 RegExpCreate ( P, F ), https://tc39.es/ecma262/#sec-regexpcreate RegExpObject* regexp_create(GlobalObject& global_object, Value pattern, Value flags) { diff --git a/Userland/Libraries/LibJS/Runtime/RegExpObject.h b/Userland/Libraries/LibJS/Runtime/RegExpObject.h index b3d73b59fe..40d0680daf 100644 --- a/Userland/Libraries/LibJS/Runtime/RegExpObject.h +++ b/Userland/Libraries/LibJS/Runtime/RegExpObject.h @@ -34,6 +34,7 @@ public: RegExpObject(Regex regex, String pattern, String flags, Object& prototype); RegExpObject* regexp_initialize(GlobalObject&, Value pattern, Value flags); + String escape_regexp_pattern() const; virtual void initialize(GlobalObject&) override; virtual ~RegExpObject() override; diff --git a/Userland/Libraries/LibJS/Runtime/RegExpPrototype.cpp b/Userland/Libraries/LibJS/Runtime/RegExpPrototype.cpp index a2f4d9c6e2..8a0bf65fdb 100644 --- a/Userland/Libraries/LibJS/Runtime/RegExpPrototype.cpp +++ b/Userland/Libraries/LibJS/Runtime/RegExpPrototype.cpp @@ -18,7 +18,6 @@ #include #include #include -#include namespace JS { @@ -56,15 +55,6 @@ RegExpPrototype::~RegExpPrototype() { } -static String escape_regexp_pattern(const RegExpObject& regexp_object) -{ - auto pattern = regexp_object.pattern(); - if (pattern.is_empty()) - return "(?:)"; - // FIXME: Check u flag and escape accordingly - return pattern.replace("\n", "\\n", true).replace("\r", "\\r", true).replace(LINE_SEPARATOR_STRING, "\\u2028", true).replace(PARAGRAPH_SEPARATOR_STRING, "\\u2029", true).replace("/", "\\/", true); -} - // 22.2.5.2.3 AdvanceStringIndex ( S, index, unicode ), https://tc39.es/ecma262/#sec-advancestringindex size_t advance_string_index(Utf16View const& string, size_t index, bool unicode) { @@ -344,7 +334,7 @@ JS_DEFINE_NATIVE_GETTER(RegExpPrototype::source) return {}; } - return js_string(vm, escape_regexp_pattern(static_cast(*regexp_object))); + return js_string(vm, static_cast(*regexp_object).escape_regexp_pattern()); } // 22.2.5.2 RegExp.prototype.exec ( string ), https://tc39.es/ecma262/#sec-regexp.prototype.exec