我正在尝试从给定页面中抓取URL和Javascript代码。所以首先我写这篇文章来确定它是否有效:
file.each_line do |line|
if line.include? ".com" || ".net" || ".org" || "edu" || "()" || "var" || "javascript" || "html" || "http" || "www"
puts "String includes above"
puts line
puts "\n"
else
puts "String doesn't include above"
puts line
puts "\n"
end
end
在给定的输出中,我得到了这个:
String doesn't include above
(function() {
String doesn't include above
js.src = "//connect.facebook.net/en_us/all.js#xfbml=1";
我没想到看到这个输出,因为我认为这些将是第一个条件。为什么'包括'表现得那样?我该如何解决?
答案 0 :(得分:3)
这不是Ruby的工作方式(或我所知道的任何语言)。您必须多次使用include?
:
if line.include?('.com') || line.include?('.net') ...
最好将Regex与String#=~
:
if line =~ /(\.com|\.net|\.org|\.edu)/i