Ruby on Rails string scan and regular expression
I wanted to get image url "http://www.test.com/image.jpg" out from the string:
"<img align=&qu开发者_开发技巧ot;right" alt="Title " src="http://www.test.com/image.jpg" width="120" /><"
Here is the code that I have:
module MyHelper
    def getMymage(allDesc)
        allDesc = "<img align="right" alt="Title " src="http://www.test.com/image.jpg" width="120" /><"
        allDesc = allDesc.scan(src="(\S+)")
    end
end
I got the following error:
syntax error, unexpected tAMPER
        allDesc = allDesc.scan(src="(\S+)")
                                    
syntax error, unexpected $undefined
        allDesc = allDesc.scan(src="(\S+)")
                                       
How to fix it?
Can't comment on sunkencity's answer, but regex that solves the dash problem is:
/src=\"([a-z0-9_.\-:\/]+)"/i
The regexp is missing a start "/" and some extra stuff
allDesc.scan(/src=\"([a-z0-9_.\-:\/]+)"/i)
but you get an array as a response:
=> [["http://www.test.com/image.jpg"]] 
I'd suggest using the matching operator and then use the first match variable:
allDesc =~ /(http:\/\/[a-z0-9_.-i\/]+)/ && $1
 
         加载中,请稍侯......
 加载中,请稍侯......
      
精彩评论