Lua:在 string.gsub 中转义文字 % 并捕获 %2
基本问题,但我已经尝试了一段时间但没有运气。
我正在处理 url,需要进行简单的替换。
我需要用文字字符串 %20
替换空格,但我似乎无法转义 % 或 %2
,它们被报告为无效捕获。
text = string.gsub(text, "%s+", '%%20')
我必须在 gsub
内部使用多少个 %
来转义 %
符号和 %2
捕获。
Basic question but I've been trying to figure out for a while with no luck.
I am processing urls and need to do a simple replacement.
I need to replace spaces with the literal string %20
, but I can't seem to escape the % or the %2
which is reported as an invalid capture.
text = string.gsub(text, "%s+", '%%20')
How many %
do I have to use inside gsub
to escape the %
sign and the %2
capture.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
似乎对我有用:
您需要显示更多代码和错误消息。
Seems to work for me:
You'll need to show some more code and your error message.
或者,您可以使用以下命令自动执行此操作:
当然,假设您的 lua 路径中确实有套接字库。老实说,这就是我对 url 执行任何操作的方式,因为这样您就不必担心将逗号转换为 %2c 或将撇号转换为 %27。
Alternatively, you can automatically do that with the following:
This is, of course, assuming you do have the socket library in your lua path. To be quite honest, this is the way I would go about doing anything with urls, because then you don't have to worry about converting commas into %2c or apostrophes into %27.