Regexp.prototype.source get error result

Issue: https://gitee.com/openharmony/arkcompiler_ets_runtime/issues/IB0NG2

Signed-off-by: hecunmao <hecunmao@huawei.com>
Change-Id: Ia5b4f03e2259596afd189a0db541102bdb3cd04d
This commit is contained in:
hecunmao 2024-10-29 20:28:10 +08:00
parent 29ed61f07d
commit 3c890df522
3 changed files with 114 additions and 5 deletions

View File

@ -2431,11 +2431,17 @@ EcmaString *BuiltinsRegExp::EscapeRegExpPattern(JSThread *thread, const JSHandle
if (srcStdStr.empty()) {
srcStdStr = "(?:)";
}
// "/" -> "\/"
srcStdStr = base::StringHelper::ReplaceAll(srcStdStr, "/", "\\/");
// "\\" -> "\"
srcStdStr = base::StringHelper::ReplaceAll(srcStdStr, "\\", "\\");
bool escapeChar = false;
for (int i = 0; i < srcStdStr.size(); i++) {
if (srcStdStr[i] == '\\') {
escapeChar=!escapeChar;
} else if (!escapeChar && srcStdStr[i]=='/') {
srcStdStr.insert(i, "\\");
i++;
} else {
escapeChar = false;
}
}
return *factory->NewFromUtf8(srcStdStr);
}

View File

@ -176,3 +176,19 @@ gs
gu
dg
g
\/ ["/"]
\/[a-z]*> ["/style>"]
\/[a-z]*> ["/style>"]
\/[a-z]*> ["/style>"]
\\\/[a-z]*> ["\\/style>"]
\\\/[a-z]*> ["\\/style>"]
\\\/[a-z]*> ["\\/style>"]
\\\/[a-z]*> ["\\/style>"]
SyntaxError
SyntaxError
\[ ["["]
\[ ["["]
SyntaxError
SyntaxError
\\\[ ["\\["]
\\\[ ["\\["]

View File

@ -726,3 +726,90 @@ print(res7.flags);
const res8 = /abc/;
res8.lastIndex = -1;
print(res8.flags);
{
let str = "<style>test</style>"
let reg = new RegExp("/");
print(reg.source,JSON.stringify(reg.exec(str)));
reg = new RegExp("\/[a-z]*>");
print(reg.source,JSON.stringify(reg.exec(str)));
reg = new RegExp("\\/[a-z]*>");
print(reg.source,JSON.stringify(reg.exec(str)));
reg = new RegExp("\\\/[a-z]*>");
print(reg.source,JSON.stringify(reg.exec(str)));
str="<style>test<\\\\/style>"
reg = new RegExp("\\\\/[a-z]*>");
print(reg.source,JSON.stringify(reg.exec(str)));
reg = new RegExp("\\\\\/[a-z]*>");
print(reg.source,JSON.stringify(reg.exec(str)));
reg = new RegExp("\\\\\\/[a-z]*>");
print(reg.source,JSON.stringify(reg.exec(str)));
reg = new RegExp("\\\\\\\/[a-z]*>");
print(reg.source,JSON.stringify(reg.exec(str)));
}
{
let str="\\\\\\\\[aaa"
try {
let pattern="[";
let reg = new RegExp(pattern);
print(pattern,JSON.stringify(reg.exec(str)))
} catch (e) {
print(e.name);
}
try {
let pattern="\[";
let reg = new RegExp(pattern);
print(pattern,JSON.stringify(reg.exec(str)))
} catch (e) {
print(e.name);
}
try {
let pattern="\\[";
let reg = new RegExp(pattern);
print(pattern,JSON.stringify(reg.exec(str)))
} catch (e) {
print(e.name);
}
try {
let pattern="\\\[";
let reg = new RegExp(pattern);
print(pattern,JSON.stringify(reg.exec(str)))
} catch (e) {
print(e.name);
}
try {
let pattern="\\\\[";
let reg = new RegExp(pattern);
print(pattern,JSON.stringify(reg.exec(str)))
} catch (e) {
print(e.name);
}
try {
let pattern="\\\\\[";
let reg = new RegExp(pattern);
print(pattern,JSON.stringify(reg.exec(str)))
} catch (e) {
print(e.name);
}
try {
let pattern="\\\\\\[";
let reg = new RegExp(pattern);
print(pattern,JSON.stringify(reg.exec(str)))
} catch (e) {
print(e.name);
}
try {
let pattern="\\\\\\\[";
let reg = new RegExp(pattern);
print(pattern,JSON.stringify(reg.exec(str)))
} catch (e) {
print(e.name);
}
}