diff --git a/ecmascript/regexp/regexp_parser.cpp b/ecmascript/regexp/regexp_parser.cpp index dfebe939460e018191a00758f2ebf9c52ddaf3de..5500fd38e9f29122f3a6028f5aed796738ae2b7c 100644 --- a/ecmascript/regexp/regexp_parser.cpp +++ b/ecmascript/regexp/regexp_parser.cpp @@ -111,6 +111,10 @@ void RegExpParser::ParseDisjunction(bool isBackward) { // NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg) PrintF("Parse Disjunction------\n"); + if (c0_ == ')') { + isEmpty_ = true; + return; + } size_t start = buffer_.size_; ParseAlternative(isBackward); if (isError_) { @@ -746,7 +750,7 @@ void RegExpParser::ParseQuantifier(size_t atomBcStart, int captureStart, int cap ParseError("nothing to repeat"); return; } - if (min != -1 && max != -1) { + if (min != -1 && max != -1 && !isEmpty_) { stackCount_++; PushOpCode pushOp; pushOp.InsertOpCode(&buffer_, atomBcStart); @@ -788,6 +792,7 @@ void RegExpParser::ParseQuantifier(size_t atomBcStart, int captureStart, int cap PopOpCode popOp; popOp.EmitOpCode(&buffer_); } + isEmpty_ = false; } bool RegExpParser::ParseGroupSpecifier(const uint8_t **pp, CString &name) diff --git a/ecmascript/regexp/regexp_parser.h b/ecmascript/regexp/regexp_parser.h index e3f6afa43673879c704a4ac4d8d3767531193fac..4c4f91af1f4d8ef8e2879bf23fc347f1b3bf7f8e 100644 --- a/ecmascript/regexp/regexp_parser.h +++ b/ecmascript/regexp/regexp_parser.h @@ -65,6 +65,7 @@ public: captureCount_(0), stackCount_(0), isError_(false), + isEmpty_(false), buffer_(chunk), groupNames_(chunk) { @@ -216,6 +217,7 @@ private: end_ = nullptr; c0_ = KEY_EOF; isError_ = false; + isEmpty_ = false; } void Advance() @@ -259,6 +261,7 @@ private: int captureCount_; int stackCount_; bool isError_; + bool isEmpty_; char errorMsg_[TMP_BUF_SIZE] = {0}; // NOLINTNEXTLINE(modernize-avoid-c-arrays) int hasNamedCaptures_ = -1; int totalCaptureCount_ = -1; diff --git a/test/moduletest/regexp/expect_output.txt b/test/moduletest/regexp/expect_output.txt index 3017f68013d29b9568c4158e904f440690ac791b..be59f42322f268c5cfcbfb8fbb306bbd4aa935a7 100644 --- a/test/moduletest/regexp/expect_output.txt +++ b/test/moduletest/regexp/expect_output.txt @@ -25,3 +25,5 @@ false true false SC52BAHL01031234567890123456USD +true + diff --git a/test/moduletest/regexp/regexp.js b/test/moduletest/regexp/regexp.js index 1774fb810c5a6c50256c741a51ebda7a33d50122..7ff5ea0eb8f5b7f162cfd9956f932aa6ba412d2a 100644 --- a/test/moduletest/regexp/regexp.js +++ b/test/moduletest/regexp/regexp.js @@ -283,4 +283,11 @@ print(temp1) let str1 = 'SC52BAHL01031234567890123456USD' print(str1.replace(/[^A-Z0-9]+/gi, '')) -let reg50 = /^[ABCEGHJKLMNPRSTVXY]\d[ABCEGHJ-NPRSTV-Z][\s-]?\d[ABCEGHJ-NPRSTV-Z]\d$/i \ No newline at end of file +let reg50 = /^[ABCEGHJKLMNPRSTVXY]\d[ABCEGHJ-NPRSTV-Z][\s-]?\d[ABCEGHJ-NPRSTV-Z]\d$/i + +const regex = /(?:)+/; +const str10 = "abcabcabc"; +const matches = regex.test(str10); +print(matches); +const matches1 = regex.exec(str10); +print(matches1); \ No newline at end of file