I am using this code toe place YouTube URLs with an icon that when you click on it, it opens a lightbox that shows the video.
Here's the C# code:
const string pattern = @"(?:https?:\/\/)?(?:www\.)?(?:(?:(?:youtube.com\/watch\?[^?]*v=|youtu.be\/)([\w\-]+))(?:[^\s?]+)?)";
const string replacement = "<a title='Click to watch the video' rel='nofollow' class='youtube-popup' href='//www.youtube.com/watch?v=$1' data-lity><span class='fa fa-play'></span>Watch</a>";
var rgx = new Regex(pattern);
var result = rgx.Replace(theinput, replacement);
if(result != null && result != "")
{
return result;
}
The code replaces the video URLs and shows the icons, but also cuts the HTML after it (<p class="tags"></p>
) and it gets cut to class='tags'>
(both the paragraph tags are not present, and because of that, it places in an element before it that contains the links.
I tested it with two links in the same paragraph, separated with text and spaces between them of course.
How can I changed the Regex to work and not break the HTML for this particular example?