Ruby regular expression using variable name

后端 未结 5 1605
难免孤独
难免孤独 2020-11-28 04:08

Is is possible to create/use a regular expression pattern in ruby that is based on the value of a variable name?

For instance, we all know we can do the following wi

5条回答
  •  悲&欢浪女
    2020-11-28 04:54

    You can use regular expressions through variables in ruby:

    var = /Value/
    str = "a test Value"
    str.gsub( /#{var}/, 'foo' )
    

提交回复
热议问题