diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index 76547b8c3..778e43355 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -212,11 +212,10 @@ module ApplicationHelper end def process_youtube(message, youtube_maps) - regex = /( |^)(https?:\/\/)?www\.youtube\.com\/watch[^ ]*v=([A-Za-z0-9_\-]+)(&[^ ]*)?(#[^ ]+)?/ - processed_message = message.gsub(regex) do |matched_string| - match_data = matched_string.match(regex) - video_id = match_data[3] - anchor = match_data[5] + processed_message = message.gsub(YoutubeTitles::YOUTUBE_ID_REGEX) do |matched_string| + match_data = matched_string.match(YoutubeTitles::YOUTUBE_ID_REGEX) + video_id = match_data[1] + anchor = match_data[2] anchor ||= '' if youtube_maps && youtube_maps[video_id] title = h(CGI::unescape(youtube_maps[video_id])) diff --git a/lib/youtube_titles.rb b/lib/youtube_titles.rb index 7262da5fe..760cb703b 100644 --- a/lib/youtube_titles.rb +++ b/lib/youtube_titles.rb @@ -20,5 +20,5 @@ module YoutubeTitles end end - YOUTUBE_ID_REGEX = /(?:youtu\.be\/|(?:[a-z]{2,3}\.)?youtube\.com\/watch(?:\?|#!|.+&|.+&)v=)([\w-]{11})/im unless defined? YOUTUBE_ID_REGEX + YOUTUBE_ID_REGEX = /(?:youtu\.be\/|(?:[a-z]{2,3}\.)?youtube\.com\/watch(?:\?|#!|.+&|.+&)v=)([\w-]{11})(?:\S*(#[^ ]+))?/im unless defined? YOUTUBE_ID_REGEX end