我的代码没问题,但 Lua 的替换不起作用(:gsub)

大家好,我想把一个特定的文本替换成"",但我的代码不起作用。我不知道为什么我的代码不起作用。

b = '只是测试。<script>window.location.replace("http://google.com");</script>'
print(b)
b = b:gsub('<script>window.location.replace("http://google.com");</script>', "")
print(b)

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

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

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

我在 FiveM 中工作

原文链接 https://stackoverflow.com/questions/70086110

点赞
stackoverflow用户7396148
stackoverflow用户7396148

你需要转义 () ,因为在 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 模式的信息,您可以查看以下资源:

理解 Lua 模式

20.1 - 模式匹配函数

20.2 - 模式

2021-11-23 18:39:01