0

gsub を正しく使用できません。

このコードを考えると:

"replace me".gsub(/replace me/, "this \\0 is a test")

結果は次のとおりです。

 "this replace me is a test" 

しかし、私が期待しているのは次のとおりです。

"this \0 is a test"

gsub を使用して必要な結果を得るにはどうすればよいですか?

4

1 に答える 1

4

別の円記号でエスケープして、必要なことgsubがわかるようにします"\\0"

"replace me".gsub(/replace me/, "this \\\\0 is a test")

(編集)"\0"バイトを意味する場合は、次のようにします0x00

"replace me".gsub(/replace me/, "this \0 is a test")
于 2010-07-27T02:38:15.680 に答える