开发者

How can I interpolate a variable in a Ruby regex?

开发者 https://www.devze.com 2023-03-21 07:13 出处:网络
data.to_en开发者_开发问答um(:scan,/(#entity[0])/i).map do |m,| p $`.size How can I use dynamic variable in regular expression?
 data.to_en开发者_开发问答um(:scan,/(#entity[0])/i).map do |m,|
        p $`.size

How can I use dynamic variable in regular expression? #entity[0] returns a value, but in the above syntax #entity[0] is taken literally in the regex.


You want /#{entity[0]}/i. #{} is the syntax for variable insertion in strings and regexes.

0

精彩评论

暂无评论...
验证码 换一张
取 消