字符串中删除反斜杠

字符串中删除反斜杠

我有一根这样的绳子

a="[\"6000208900\",\"600020890225\",\"600900231930\"]"
#expected result [6000208900,600020890225,600900231930]

我正试图从字符串中删除反斜杠。
a.gsub!(/^\"|\"?$/, '')

最佳答案

Inside the double quoted string(""),另一个双引号必须由\转义。你不能移除它。
使用puts,您可以看到它不在那里。

a = "[\"6000208902912790\"]"
puts a # => ["6000208902912790"]

或使用JSON
irb(main):001:0> require 'json'
=> true
irb(main):002:0> a = "[\"6000208902912790\"]"
=> "[\"6000208902912790\"]"
irb(main):003:0> b = JSON.parse a
=> ["6000208902912790"]
irb(main):004:0> b
=> ["6000208902912790"]
irb(main):005:0> b.to_s
=> "[\"6000208902912790\"]"

更新(根据OP的最后一次编辑)
irb(main):002:0> a = "[\"6000208900\",\"600020890225\",\"600900231930\"]"
=> "[\"6000208900\",\"600020890225\",\"600900231930\"]"
irb(main):006:0> a.scan(/\d+/).map(&:to_i)
=> [6000208900, 600020890225, 600900231930]
irb(main):007:0>

07-25 22:31