Ruby replace string with captured regex pattern

后端 未结 6 1896
庸人自扰
庸人自扰 2021-01-31 13:13

I am having trouble translating this into Ruby.

Here is a piece of JavaScript that does exactly what I want to do:

function get_code(str){
    return str         


        
6条回答
  •  有刺的猬
    2021-01-31 13:36

    def get_code(str)
      str.sub(/^(Z_.*): .*/, '\1')
    end
    get_code('Z_foo: bar!') # => "Z_foo"
    

提交回复
热议问题