【问题标题】:my code is right but Lua replace doesn't working (:gsub)我的代码是正确的,但 Lua 替换不起作用(:gsub)
【发布时间】:2022-01-02 06:06:09
【问题描述】:

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

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,'&lt;script&gt;window.location.replace("http://google.com");&lt;/script&gt;',""),但它也不起作用

我在 FiveM 工作

【问题讨论】:

    标签: string replace lua


    【解决方案1】:

    您需要转义 (),在 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

    【讨论】:

    • 您好,感谢您的帮助,但您的代码也无法正常工作,而且我的英语水平也不足以理解此文档,您能否给我发送另一个可以工作的代码
    • 你好,这是我的错,你的代码正在运行,对不起
    • @Mordexx 你的母语是什么?我也许能找到更好的资源来专门帮助你
    • 你可能不会发现,因为我的语言不熟悉。它的土耳其语。感谢您的帮助
    • 是的......我认为你是对的,对不起。
    猜你喜欢
    • 1970-01-01
    • 2017-03-26
    • 2017-12-24
    • 2020-03-27
    • 2021-12-04
    • 2015-02-04
    • 1970-01-01
    • 1970-01-01
    • 2022-07-08
    相关资源
    最近更新 更多