string.gsub 中带有反斜杠的错误
local a = "te\st"
local b = string.gsub(a,'\','\\\\')
assert(false,b)
我做错了什么?
当我执行 assert
时,我希望在屏幕上打印字符串 te\st
...但是它不起作用
我有一个 JSON 文件,我想要将其解码到Lua表中。我不需要打印任何内容,我执行断言只是为了测试本地问题。
所以我需要的是将所有数据保留在包含 '\'
的 JSON 文件中。
local a = "te\st"
local b = string.gsub(a,'\','\\\\')
assert(false,b)
What am I doing wrong?
When I do assert
, I want that to the screen the string te\st
will be printed... but it's not working
I have a JSON file, that I want to decode it into Lua table. I don't need to print out nothing, I did the assert
just to test a local problem.
So what I need is to keep all data in the JSON file that has '\'
.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果您不希望反斜杠具有特殊含义,请使用
[[]]
而不是""
或''
。阅读手册中有关文字字符串的内容。
Use
[[]]
instead of""
or''
if you don't want backslash to have special meaning.Read about literal strings in the manual.
你有没有尝试过用
%
字符而不是\
来转义它我不知道这是否有帮助,但我在制作 Lua 的
时经历了一段痛苦的时光gsub
将我的字符串与其中的特殊字符进行匹配,我希望按字面意思进行处理...事实证明,我不需要使用\
作为转义字符或加倍该字符,而是需要在特殊字符前添加%
使其按字面意思处理。Have you tried escaping it with the
%
character instead of\
I don't know if this will help, but I was having a HELL of a time making Lua's
gsub
match my string with special characters in it that I wanted treated literally... it turned out that instead of using\
as an escape character, or doubling the character, that I needed to prefix the special character with%
to make it be treated literally.你的问题不太清楚,所以我不能100%确定你的意思。您的意思是希望当 b 等于字符串“te\st”时触发断言吗?如果是这样,你可以做一个简单的:
或者我猜......
Your question wasn't too clear so I'm not 100% sure what you mean. Do you mean that you want the assert to fire when b is equal to the string "te\st"? If so you can do a simple:
Or I suppse...
你不需要gsub。但无论如何,它就在这里。
You don't need the gsub. But here it is anyways.