Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我在正确使用 gsub 时遇到问题:
鉴于此代码:
"replace me".gsub(/replace me/, "this \\0 is a test")
结果是:
"this replace me is a test"
但我期待的是:
"this \0 is a test"
如何使用 gsub 获得我想要的结果?
用另一个反斜杠转义它,这样gsub就知道你想要了"\\0"。
gsub
"\\0"
"replace me".gsub(/replace me/, "this \\\\0 is a test")
(编辑)如果"\0"您的意思是 byte 0x00,请执行以下操作:
"\0"
0x00
"replace me".gsub(/replace me/, "this \0 is a test")