Regular expression in ruby on rails to match youtube.com and youtu.be url in string

    data = 'string with youtube.com and youtu.be  urls'
    pattern = '/(?:https?:\/\/)?(?:www\.)?youtu(?:\.be|be\.com)\/(?:watch\?v=)?(\w{10,11})/i'

    // For example replace links with id

    newdata = data.gsub(pattern, "#{$2}")
    5
    Average: 5 (1 vote)