小编典典

将字符串插入正则表达式

all

我需要将字符串的值替换为我在 Ruby 中的正则表达式。是否有捷径可寻?例如:

foo = "0.0.0.0"
goo = "here is some other stuff 0.0.0.0" 
if goo =~ /value of foo here dynamically/
  puts "success!"
end

阅读 113

收藏
2022-08-27

共1个答案

小编典典

与字符串插入相同。

if goo =~ /#{Regexp.quote(foo)}/
#...
2022-08-27