我的代码是正确的,但 Lua 替换不起作用 (:gsub)

my code is right but Lua replace doesn't working (:gsub)

您好,我正在尝试将特定文本替换为“”,但我的代码无法正常工作。我只是不知道为什么我的代码不起作用

b = 'Just testing.<script>window.location.replace("http://google.com");</script>'
print(b)
b = b:gsub('<script>window.location.replace("http://google.com");</script>', "")
print(b)

输出 1:只是 testing.window.location.replace("http://google.com");
输出 2:只是 testing.window.location.replace("http://google.com");

我也试过b = string.gsub(b,'<script>window.location.replace("http://google.com");</script>',""),但也没用

我在 FiveM 工作

您需要转义 (),在 lua 模式中它们被识别为特殊字符。你可以使用 %

来逃避它们
b = 'Just testing.<script>window.location.replace("http://google.com");</script>'
print(b)
b = b:gsub('<script>window.location.replace%("http://google.com"%);</script>', "")
print(b)

有关 lua 模式的更多信息,您可以查看这些资源:

Understanding Lua Patterns

20.1 – Pattern-Matching Functions

20.2 – Patterns