开发者

How can I interpolate a variable in a Ruby regex?

 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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜